blob: b8cccd126c3cf3b5988ab04c7a8869baef515c1c [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.
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());
minyue0de1c132016-03-17 02:39:30 -0700184 RTC_CHECK(msg.has_aec_extended_filter_enabled());
minyue0de1c132016-03-17 02:39:30 -0700185
minyue0de1c132016-03-17 02:39:30 -0700186 // We only create APM once, since changes on these fields should not
187 // happen in current implementation.
188 if (!apm_.get()) {
Per Åhgrencc73ed32020-04-26 23:56:17 +0200189 apm_.reset(AudioProcessingBuilderForTesting().Create(config));
minyue0de1c132016-03-17 02:39:30 -0700190 }
191}
192
193void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) {
peah8271d042016-11-22 07:24:52 -0800194 AudioProcessing::Config apm_config;
195
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200196 // AEC2/AECM configs.
minyue0de1c132016-03-17 02:39:30 -0700197 RTC_CHECK(msg.has_aec_enabled());
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200198 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();
minyue0de1c132016-03-17 02:39:30 -0700201
Sam Zackrisson23513132019-01-11 15:10:32 +0100202 // 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
Per Åhgrenc0734712020-01-02 15:15:36 +0100220 // TS configs.
221 RTC_CHECK(msg.has_transient_suppression_enabled());
222 apm_config.transient_suppression.enabled =
223 msg.transient_suppression_enabled();
224
minyue0de1c132016-03-17 02:39:30 -0700225 // AGC configs.
226 RTC_CHECK(msg.has_agc_enabled());
minyue0de1c132016-03-17 02:39:30 -0700227 RTC_CHECK(msg.has_agc_mode());
minyue0de1c132016-03-17 02:39:30 -0700228 RTC_CHECK(msg.has_agc_limiter_enabled());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200229 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();
Per Åhgren0695df12020-01-13 14:43:13 +0100234 RTC_CHECK(msg.has_noise_robust_agc_enabled());
235 apm_config.gain_controller1.analog_gain_controller.enabled =
236 msg.noise_robust_agc_enabled();
Sam Zackrisson41478c72019-10-15 10:10:26 +0200237
238 apm_->ApplyConfig(apm_config);
minyue0de1c132016-03-17 02:39:30 -0700239}
240
241void DebugDumpReplayer::LoadNextMessage() {
242 has_next_event_ =
243 debug_file_ && ReadMessageFromFile(debug_file_, &next_event_);
244}
245
246} // namespace test
247} // namespace webrtc