blob: 26ca4290c36eee2692c7e037e2bf8d663fc8e487 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_processing/test/protobuf_utils.h"
Alex Loiko62347222018-09-10 10:18:07 +020014#include "modules/audio_processing/test/runtime_setting_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
minyue0de1c132016-03-17 02:39:30 -070016
17namespace webrtc {
18namespace test {
19
20namespace {
21
22void MaybeResetBuffer(std::unique_ptr<ChannelBuffer<float>>* buffer,
23 const StreamConfig& config) {
24 auto& buffer_ref = *buffer;
25 if (!buffer_ref.get() || buffer_ref->num_frames() != config.num_frames() ||
26 buffer_ref->num_channels() != config.num_channels()) {
Yves Gerey665174f2018-06-19 15:03:05 +020027 buffer_ref.reset(
28 new ChannelBuffer<float>(config.num_frames(), config.num_channels()));
minyue0de1c132016-03-17 02:39:30 -070029 }
30}
31
32} // namespace
33
34DebugDumpReplayer::DebugDumpReplayer()
35 : input_(nullptr), // will be created upon usage.
36 reverse_(nullptr),
37 output_(nullptr),
38 apm_(nullptr),
39 debug_file_(nullptr) {}
40
41DebugDumpReplayer::~DebugDumpReplayer() {
42 if (debug_file_)
43 fclose(debug_file_);
44}
45
46bool DebugDumpReplayer::SetDumpFile(const std::string& filename) {
47 debug_file_ = fopen(filename.c_str(), "rb");
48 LoadNextMessage();
49 return debug_file_;
50}
51
52// Get next event that has not run.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020053absl::optional<audioproc::Event> DebugDumpReplayer::GetNextEvent() const {
minyue0de1c132016-03-17 02:39:30 -070054 if (!has_next_event_)
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020055 return absl::nullopt;
minyue0de1c132016-03-17 02:39:30 -070056 else
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010057 return next_event_;
minyue0de1c132016-03-17 02:39:30 -070058}
59
60// Run the next event. Returns the event type.
61bool DebugDumpReplayer::RunNextEvent() {
62 if (!has_next_event_)
63 return false;
64 switch (next_event_.type()) {
65 case audioproc::Event::INIT:
66 OnInitEvent(next_event_.init());
67 break;
68 case audioproc::Event::STREAM:
69 OnStreamEvent(next_event_.stream());
70 break;
71 case audioproc::Event::REVERSE_STREAM:
72 OnReverseStreamEvent(next_event_.reverse_stream());
73 break;
74 case audioproc::Event::CONFIG:
75 OnConfigEvent(next_event_.config());
76 break;
Alex Loiko62347222018-09-10 10:18:07 +020077 case audioproc::Event::RUNTIME_SETTING:
78 OnRuntimeSettingEvent(next_event_.runtime_setting());
79 break;
minyue0de1c132016-03-17 02:39:30 -070080 case audioproc::Event::UNKNOWN_EVENT:
81 // We do not expect to receive UNKNOWN event.
Alex Loiko62347222018-09-10 10:18:07 +020082 RTC_CHECK(false);
minyue0de1c132016-03-17 02:39:30 -070083 return false;
84 }
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()) {
Ivo Creusen62337e52018-01-09 14:17:33 +0100188 apm_.reset(AudioProcessingBuilder().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