blob: 754b42d281845bb245c3990413b79562a84d9745 [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
Per Åhgrencc73ed32020-04-26 23:56:17 +020013#include "modules/audio_processing/test/audio_processing_builder_for_testing.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.
Karl Wibergc95b9392020-11-08 00:49:37 +010083 RTC_CHECK_NOTREACHED();
minyue0de1c132016-03-17 02:39:30 -070084 }
85 LoadNextMessage();
86 return true;
87}
88
89const ChannelBuffer<float>* DebugDumpReplayer::GetOutput() const {
90 return output_.get();
91}
92
93StreamConfig DebugDumpReplayer::GetOutputConfig() const {
94 return output_config_;
95}
96
97// OnInitEvent reset the input/output/reserve channel format.
98void DebugDumpReplayer::OnInitEvent(const audioproc::Init& msg) {
99 RTC_CHECK(msg.has_num_input_channels());
100 RTC_CHECK(msg.has_output_sample_rate());
101 RTC_CHECK(msg.has_num_output_channels());
102 RTC_CHECK(msg.has_reverse_sample_rate());
103 RTC_CHECK(msg.has_num_reverse_channels());
104
105 input_config_ = StreamConfig(msg.sample_rate(), msg.num_input_channels());
106 output_config_ =
107 StreamConfig(msg.output_sample_rate(), msg.num_output_channels());
108 reverse_config_ =
109 StreamConfig(msg.reverse_sample_rate(), msg.num_reverse_channels());
110
111 MaybeResetBuffer(&input_, input_config_);
112 MaybeResetBuffer(&output_, output_config_);
113 MaybeResetBuffer(&reverse_, reverse_config_);
114}
115
116// OnStreamEvent replays an input signal and verifies the output.
117void DebugDumpReplayer::OnStreamEvent(const audioproc::Stream& msg) {
118 // APM should have been created.
119 RTC_CHECK(apm_.get());
120
Sam Zackrisson41478c72019-10-15 10:10:26 +0200121 apm_->set_stream_analog_level(msg.level());
minyue0de1c132016-03-17 02:39:30 -0700122 RTC_CHECK_EQ(AudioProcessing::kNoError,
123 apm_->set_stream_delay_ms(msg.delay()));
124
minyue0de1c132016-03-17 02:39:30 -0700125 if (msg.has_keypress()) {
126 apm_->set_stream_key_pressed(msg.keypress());
127 } else {
128 apm_->set_stream_key_pressed(true);
129 }
130
131 RTC_CHECK_EQ(input_config_.num_channels(),
132 static_cast<size_t>(msg.input_channel_size()));
133 RTC_CHECK_EQ(input_config_.num_frames() * sizeof(float),
134 msg.input_channel(0).size());
135
136 for (int i = 0; i < msg.input_channel_size(); ++i) {
137 memcpy(input_->channels()[i], msg.input_channel(i).data(),
138 msg.input_channel(i).size());
139 }
140
141 RTC_CHECK_EQ(AudioProcessing::kNoError,
142 apm_->ProcessStream(input_->channels(), input_config_,
143 output_config_, output_->channels()));
144}
145
146void DebugDumpReplayer::OnReverseStreamEvent(
147 const audioproc::ReverseStream& msg) {
148 // APM should have been created.
149 RTC_CHECK(apm_.get());
150
151 RTC_CHECK_GT(msg.channel_size(), 0);
152 RTC_CHECK_EQ(reverse_config_.num_channels(),
153 static_cast<size_t>(msg.channel_size()));
154 RTC_CHECK_EQ(reverse_config_.num_frames() * sizeof(float),
155 msg.channel(0).size());
156
157 for (int i = 0; i < msg.channel_size(); ++i) {
158 memcpy(reverse_->channels()[i], msg.channel(i).data(),
159 msg.channel(i).size());
160 }
161
162 RTC_CHECK_EQ(
163 AudioProcessing::kNoError,
164 apm_->ProcessReverseStream(reverse_->channels(), reverse_config_,
165 reverse_config_, reverse_->channels()));
166}
167
168void DebugDumpReplayer::OnConfigEvent(const audioproc::Config& msg) {
169 MaybeRecreateApm(msg);
170 ConfigureApm(msg);
171}
172
Alex Loiko62347222018-09-10 10:18:07 +0200173void DebugDumpReplayer::OnRuntimeSettingEvent(
174 const audioproc::RuntimeSetting& msg) {
175 RTC_CHECK(apm_.get());
176 ReplayRuntimeSetting(apm_.get(), msg);
177}
178
minyue0de1c132016-03-17 02:39:30 -0700179void DebugDumpReplayer::MaybeRecreateApm(const audioproc::Config& msg) {
180 // These configurations cannot be changed on the fly.
181 Config config;
182 RTC_CHECK(msg.has_aec_delay_agnostic_enabled());
minyue0de1c132016-03-17 02:39:30 -0700183 RTC_CHECK(msg.has_aec_extended_filter_enabled());
minyue0de1c132016-03-17 02:39:30 -0700184
minyue0de1c132016-03-17 02:39:30 -0700185 // We only create APM once, since changes on these fields should not
186 // happen in current implementation.
187 if (!apm_.get()) {
Per Åhgrencc73ed32020-04-26 23:56:17 +0200188 apm_.reset(AudioProcessingBuilderForTesting().Create(config));
minyue0de1c132016-03-17 02:39:30 -0700189 }
190}
191
192void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) {
peah8271d042016-11-22 07:24:52 -0800193 AudioProcessing::Config apm_config;
194
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200195 // AEC2/AECM configs.
minyue0de1c132016-03-17 02:39:30 -0700196 RTC_CHECK(msg.has_aec_enabled());
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200197 RTC_CHECK(msg.has_aecm_enabled());
198 apm_config.echo_canceller.enabled = msg.aec_enabled() || msg.aecm_enabled();
199 apm_config.echo_canceller.mobile_mode = msg.aecm_enabled();
minyue0de1c132016-03-17 02:39:30 -0700200
Sam Zackrisson23513132019-01-11 15:10:32 +0100201 // HPF configs.
202 RTC_CHECK(msg.has_hpf_enabled());
203 apm_config.high_pass_filter.enabled = msg.hpf_enabled();
204
205 // Preamp configs.
206 RTC_CHECK(msg.has_pre_amplifier_enabled());
207 apm_config.pre_amplifier.enabled = msg.pre_amplifier_enabled();
208 apm_config.pre_amplifier.fixed_gain_factor =
209 msg.pre_amplifier_fixed_gain_factor();
210
211 // NS configs.
212 RTC_CHECK(msg.has_ns_enabled());
213 RTC_CHECK(msg.has_ns_level());
214 apm_config.noise_suppression.enabled = msg.ns_enabled();
215 apm_config.noise_suppression.level =
216 static_cast<AudioProcessing::Config::NoiseSuppression::Level>(
217 msg.ns_level());
218
Per Åhgrenc0734712020-01-02 15:15:36 +0100219 // TS configs.
220 RTC_CHECK(msg.has_transient_suppression_enabled());
221 apm_config.transient_suppression.enabled =
222 msg.transient_suppression_enabled();
223
minyue0de1c132016-03-17 02:39:30 -0700224 // AGC configs.
225 RTC_CHECK(msg.has_agc_enabled());
minyue0de1c132016-03-17 02:39:30 -0700226 RTC_CHECK(msg.has_agc_mode());
minyue0de1c132016-03-17 02:39:30 -0700227 RTC_CHECK(msg.has_agc_limiter_enabled());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200228 apm_config.gain_controller1.enabled = msg.agc_enabled();
229 apm_config.gain_controller1.mode =
230 static_cast<AudioProcessing::Config::GainController1::Mode>(
231 msg.agc_mode());
232 apm_config.gain_controller1.enable_limiter = msg.agc_limiter_enabled();
Per Åhgren0695df12020-01-13 14:43:13 +0100233 RTC_CHECK(msg.has_noise_robust_agc_enabled());
234 apm_config.gain_controller1.analog_gain_controller.enabled =
235 msg.noise_robust_agc_enabled();
Sam Zackrisson41478c72019-10-15 10:10:26 +0200236
237 apm_->ApplyConfig(apm_config);
minyue0de1c132016-03-17 02:39:30 -0700238}
239
240void DebugDumpReplayer::LoadNextMessage() {
241 has_next_event_ =
242 debug_file_ && ReadMessageFromFile(debug_file_, &next_event_);
243}
244
245} // namespace test
246} // namespace webrtc