blob: c7767f75e8dd06499756a5fb24bb9a5278e51218 [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"
14#include "rtc_base/checks.h"
minyue0de1c132016-03-17 02:39:30 -070015
16namespace webrtc {
17namespace test {
18
19namespace {
20
21void MaybeResetBuffer(std::unique_ptr<ChannelBuffer<float>>* buffer,
22 const StreamConfig& config) {
23 auto& buffer_ref = *buffer;
24 if (!buffer_ref.get() || buffer_ref->num_frames() != config.num_frames() ||
25 buffer_ref->num_channels() != config.num_channels()) {
Yves Gerey665174f2018-06-19 15:03:05 +020026 buffer_ref.reset(
27 new ChannelBuffer<float>(config.num_frames(), config.num_channels()));
minyue0de1c132016-03-17 02:39:30 -070028 }
29}
30
31} // namespace
32
33DebugDumpReplayer::DebugDumpReplayer()
34 : input_(nullptr), // will be created upon usage.
35 reverse_(nullptr),
36 output_(nullptr),
37 apm_(nullptr),
38 debug_file_(nullptr) {}
39
40DebugDumpReplayer::~DebugDumpReplayer() {
41 if (debug_file_)
42 fclose(debug_file_);
43}
44
45bool DebugDumpReplayer::SetDumpFile(const std::string& filename) {
46 debug_file_ = fopen(filename.c_str(), "rb");
47 LoadNextMessage();
48 return debug_file_;
49}
50
51// Get next event that has not run.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020052absl::optional<audioproc::Event> DebugDumpReplayer::GetNextEvent() const {
minyue0de1c132016-03-17 02:39:30 -070053 if (!has_next_event_)
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020054 return absl::nullopt;
minyue0de1c132016-03-17 02:39:30 -070055 else
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010056 return next_event_;
minyue0de1c132016-03-17 02:39:30 -070057}
58
59// Run the next event. Returns the event type.
60bool DebugDumpReplayer::RunNextEvent() {
61 if (!has_next_event_)
62 return false;
63 switch (next_event_.type()) {
64 case audioproc::Event::INIT:
65 OnInitEvent(next_event_.init());
66 break;
67 case audioproc::Event::STREAM:
68 OnStreamEvent(next_event_.stream());
69 break;
70 case audioproc::Event::REVERSE_STREAM:
71 OnReverseStreamEvent(next_event_.reverse_stream());
72 break;
73 case audioproc::Event::CONFIG:
74 OnConfigEvent(next_event_.config());
75 break;
76 case audioproc::Event::UNKNOWN_EVENT:
77 // We do not expect to receive UNKNOWN event.
78 return false;
79 }
80 LoadNextMessage();
81 return true;
82}
83
84const ChannelBuffer<float>* DebugDumpReplayer::GetOutput() const {
85 return output_.get();
86}
87
88StreamConfig DebugDumpReplayer::GetOutputConfig() const {
89 return output_config_;
90}
91
92// OnInitEvent reset the input/output/reserve channel format.
93void DebugDumpReplayer::OnInitEvent(const audioproc::Init& msg) {
94 RTC_CHECK(msg.has_num_input_channels());
95 RTC_CHECK(msg.has_output_sample_rate());
96 RTC_CHECK(msg.has_num_output_channels());
97 RTC_CHECK(msg.has_reverse_sample_rate());
98 RTC_CHECK(msg.has_num_reverse_channels());
99
100 input_config_ = StreamConfig(msg.sample_rate(), msg.num_input_channels());
101 output_config_ =
102 StreamConfig(msg.output_sample_rate(), msg.num_output_channels());
103 reverse_config_ =
104 StreamConfig(msg.reverse_sample_rate(), msg.num_reverse_channels());
105
106 MaybeResetBuffer(&input_, input_config_);
107 MaybeResetBuffer(&output_, output_config_);
108 MaybeResetBuffer(&reverse_, reverse_config_);
109}
110
111// OnStreamEvent replays an input signal and verifies the output.
112void DebugDumpReplayer::OnStreamEvent(const audioproc::Stream& msg) {
113 // APM should have been created.
114 RTC_CHECK(apm_.get());
115
116 RTC_CHECK_EQ(AudioProcessing::kNoError,
117 apm_->gain_control()->set_stream_analog_level(msg.level()));
118 RTC_CHECK_EQ(AudioProcessing::kNoError,
119 apm_->set_stream_delay_ms(msg.delay()));
120
121 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
122 if (msg.has_keypress()) {
123 apm_->set_stream_key_pressed(msg.keypress());
124 } else {
125 apm_->set_stream_key_pressed(true);
126 }
127
128 RTC_CHECK_EQ(input_config_.num_channels(),
129 static_cast<size_t>(msg.input_channel_size()));
130 RTC_CHECK_EQ(input_config_.num_frames() * sizeof(float),
131 msg.input_channel(0).size());
132
133 for (int i = 0; i < msg.input_channel_size(); ++i) {
134 memcpy(input_->channels()[i], msg.input_channel(i).data(),
135 msg.input_channel(i).size());
136 }
137
138 RTC_CHECK_EQ(AudioProcessing::kNoError,
139 apm_->ProcessStream(input_->channels(), input_config_,
140 output_config_, output_->channels()));
141}
142
143void DebugDumpReplayer::OnReverseStreamEvent(
144 const audioproc::ReverseStream& msg) {
145 // APM should have been created.
146 RTC_CHECK(apm_.get());
147
148 RTC_CHECK_GT(msg.channel_size(), 0);
149 RTC_CHECK_EQ(reverse_config_.num_channels(),
150 static_cast<size_t>(msg.channel_size()));
151 RTC_CHECK_EQ(reverse_config_.num_frames() * sizeof(float),
152 msg.channel(0).size());
153
154 for (int i = 0; i < msg.channel_size(); ++i) {
155 memcpy(reverse_->channels()[i], msg.channel(i).data(),
156 msg.channel(i).size());
157 }
158
159 RTC_CHECK_EQ(
160 AudioProcessing::kNoError,
161 apm_->ProcessReverseStream(reverse_->channels(), reverse_config_,
162 reverse_config_, reverse_->channels()));
163}
164
165void DebugDumpReplayer::OnConfigEvent(const audioproc::Config& msg) {
166 MaybeRecreateApm(msg);
167 ConfigureApm(msg);
168}
169
170void DebugDumpReplayer::MaybeRecreateApm(const audioproc::Config& msg) {
171 // These configurations cannot be changed on the fly.
172 Config config;
173 RTC_CHECK(msg.has_aec_delay_agnostic_enabled());
174 config.Set<DelayAgnostic>(
175 new DelayAgnostic(msg.aec_delay_agnostic_enabled()));
176
177 RTC_CHECK(msg.has_noise_robust_agc_enabled());
178 config.Set<ExperimentalAgc>(
179 new ExperimentalAgc(msg.noise_robust_agc_enabled()));
180
181 RTC_CHECK(msg.has_transient_suppression_enabled());
182 config.Set<ExperimentalNs>(
183 new ExperimentalNs(msg.transient_suppression_enabled()));
184
185 RTC_CHECK(msg.has_aec_extended_filter_enabled());
186 config.Set<ExtendedFilter>(
187 new ExtendedFilter(msg.aec_extended_filter_enabled()));
188
minyue0de1c132016-03-17 02:39:30 -0700189 // We only create APM once, since changes on these fields should not
190 // happen in current implementation.
191 if (!apm_.get()) {
Ivo Creusen62337e52018-01-09 14:17:33 +0100192 apm_.reset(AudioProcessingBuilder().Create(config));
minyue0de1c132016-03-17 02:39:30 -0700193 }
194}
195
196void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) {
peah8271d042016-11-22 07:24:52 -0800197 AudioProcessing::Config apm_config;
198
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200199 // AEC2/AECM configs.
minyue0de1c132016-03-17 02:39:30 -0700200 RTC_CHECK(msg.has_aec_enabled());
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200201 RTC_CHECK(msg.has_aecm_enabled());
202 apm_config.echo_canceller.enabled = msg.aec_enabled() || msg.aecm_enabled();
203 apm_config.echo_canceller.mobile_mode = msg.aecm_enabled();
minyue0de1c132016-03-17 02:39:30 -0700204
205 RTC_CHECK(msg.has_aec_drift_compensation_enabled());
206 RTC_CHECK_EQ(AudioProcessing::kNoError,
207 apm_->echo_cancellation()->enable_drift_compensation(
208 msg.aec_drift_compensation_enabled()));
209
210 RTC_CHECK(msg.has_aec_suppression_level());
211 RTC_CHECK_EQ(AudioProcessing::kNoError,
212 apm_->echo_cancellation()->set_suppression_level(
213 static_cast<EchoCancellation::SuppressionLevel>(
214 msg.aec_suppression_level())));
215
minyue0de1c132016-03-17 02:39:30 -0700216 RTC_CHECK(msg.has_aecm_comfort_noise_enabled());
217 RTC_CHECK_EQ(AudioProcessing::kNoError,
218 apm_->echo_control_mobile()->enable_comfort_noise(
219 msg.aecm_comfort_noise_enabled()));
220
221 RTC_CHECK(msg.has_aecm_routing_mode());
222 RTC_CHECK_EQ(AudioProcessing::kNoError,
223 apm_->echo_control_mobile()->set_routing_mode(
224 static_cast<EchoControlMobile::RoutingMode>(
225 msg.aecm_routing_mode())));
226
227 // AGC configs.
228 RTC_CHECK(msg.has_agc_enabled());
229 RTC_CHECK_EQ(AudioProcessing::kNoError,
230 apm_->gain_control()->Enable(msg.agc_enabled()));
231
232 RTC_CHECK(msg.has_agc_mode());
233 RTC_CHECK_EQ(AudioProcessing::kNoError,
234 apm_->gain_control()->set_mode(
235 static_cast<GainControl::Mode>(msg.agc_mode())));
236
237 RTC_CHECK(msg.has_agc_limiter_enabled());
238 RTC_CHECK_EQ(AudioProcessing::kNoError,
239 apm_->gain_control()->enable_limiter(msg.agc_limiter_enabled()));
240
241 // HPF configs.
242 RTC_CHECK(msg.has_hpf_enabled());
peah8271d042016-11-22 07:24:52 -0800243 apm_config.high_pass_filter.enabled = msg.hpf_enabled();
minyue0de1c132016-03-17 02:39:30 -0700244
245 // NS configs.
246 RTC_CHECK(msg.has_ns_enabled());
247 RTC_CHECK_EQ(AudioProcessing::kNoError,
248 apm_->noise_suppression()->Enable(msg.ns_enabled()));
249
250 RTC_CHECK(msg.has_ns_level());
251 RTC_CHECK_EQ(AudioProcessing::kNoError,
252 apm_->noise_suppression()->set_level(
253 static_cast<NoiseSuppression::Level>(msg.ns_level())));
peah8271d042016-11-22 07:24:52 -0800254
255 apm_->ApplyConfig(apm_config);
minyue0de1c132016-03-17 02:39:30 -0700256}
257
258void DebugDumpReplayer::LoadNextMessage() {
259 has_next_event_ =
260 debug_file_ && ReadMessageFromFile(debug_file_, &next_event_);
261}
262
263} // namespace test
264} // namespace webrtc