minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/test/debug_dump_replayer.h" |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 12 | |
saza | be490b2 | 2018-10-03 17:03:13 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/echo_cancellation_impl.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_processing/test/protobuf_utils.h" |
Alex Loiko | 6234722 | 2018-09-10 10:18:07 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/test/runtime_setting_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | void 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | buffer_ref.reset( |
| 29 | new ChannelBuffer<float>(config.num_frames(), config.num_channels())); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | DebugDumpReplayer::DebugDumpReplayer() |
| 36 | : input_(nullptr), // will be created upon usage. |
| 37 | reverse_(nullptr), |
| 38 | output_(nullptr), |
| 39 | apm_(nullptr), |
| 40 | debug_file_(nullptr) {} |
| 41 | |
| 42 | DebugDumpReplayer::~DebugDumpReplayer() { |
| 43 | if (debug_file_) |
| 44 | fclose(debug_file_); |
| 45 | } |
| 46 | |
| 47 | bool 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 Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 54 | absl::optional<audioproc::Event> DebugDumpReplayer::GetNextEvent() const { |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 55 | if (!has_next_event_) |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 56 | return absl::nullopt; |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 57 | else |
Oskar Sundbom | aa8b67d | 2017-11-17 14:34:48 +0100 | [diff] [blame] | 58 | return next_event_; |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Run the next event. Returns the event type. |
| 62 | bool 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 Loiko | 6234722 | 2018-09-10 10:18:07 +0200 | [diff] [blame] | 78 | case audioproc::Event::RUNTIME_SETTING: |
| 79 | OnRuntimeSettingEvent(next_event_.runtime_setting()); |
| 80 | break; |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 81 | case audioproc::Event::UNKNOWN_EVENT: |
| 82 | // We do not expect to receive UNKNOWN event. |
Alex Loiko | 6234722 | 2018-09-10 10:18:07 +0200 | [diff] [blame] | 83 | RTC_CHECK(false); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | LoadNextMessage(); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | const ChannelBuffer<float>* DebugDumpReplayer::GetOutput() const { |
| 91 | return output_.get(); |
| 92 | } |
| 93 | |
| 94 | StreamConfig DebugDumpReplayer::GetOutputConfig() const { |
| 95 | return output_config_; |
| 96 | } |
| 97 | |
| 98 | // OnInitEvent reset the input/output/reserve channel format. |
| 99 | void 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. |
| 118 | void DebugDumpReplayer::OnStreamEvent(const audioproc::Stream& msg) { |
| 119 | // APM should have been created. |
| 120 | RTC_CHECK(apm_.get()); |
| 121 | |
| 122 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 123 | apm_->gain_control()->set_stream_analog_level(msg.level())); |
| 124 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 125 | apm_->set_stream_delay_ms(msg.delay())); |
| 126 | |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 127 | if (msg.has_keypress()) { |
| 128 | apm_->set_stream_key_pressed(msg.keypress()); |
| 129 | } else { |
| 130 | apm_->set_stream_key_pressed(true); |
| 131 | } |
| 132 | |
| 133 | RTC_CHECK_EQ(input_config_.num_channels(), |
| 134 | static_cast<size_t>(msg.input_channel_size())); |
| 135 | RTC_CHECK_EQ(input_config_.num_frames() * sizeof(float), |
| 136 | msg.input_channel(0).size()); |
| 137 | |
| 138 | for (int i = 0; i < msg.input_channel_size(); ++i) { |
| 139 | memcpy(input_->channels()[i], msg.input_channel(i).data(), |
| 140 | msg.input_channel(i).size()); |
| 141 | } |
| 142 | |
| 143 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 144 | apm_->ProcessStream(input_->channels(), input_config_, |
| 145 | output_config_, output_->channels())); |
| 146 | } |
| 147 | |
| 148 | void DebugDumpReplayer::OnReverseStreamEvent( |
| 149 | const audioproc::ReverseStream& msg) { |
| 150 | // APM should have been created. |
| 151 | RTC_CHECK(apm_.get()); |
| 152 | |
| 153 | RTC_CHECK_GT(msg.channel_size(), 0); |
| 154 | RTC_CHECK_EQ(reverse_config_.num_channels(), |
| 155 | static_cast<size_t>(msg.channel_size())); |
| 156 | RTC_CHECK_EQ(reverse_config_.num_frames() * sizeof(float), |
| 157 | msg.channel(0).size()); |
| 158 | |
| 159 | for (int i = 0; i < msg.channel_size(); ++i) { |
| 160 | memcpy(reverse_->channels()[i], msg.channel(i).data(), |
| 161 | msg.channel(i).size()); |
| 162 | } |
| 163 | |
| 164 | RTC_CHECK_EQ( |
| 165 | AudioProcessing::kNoError, |
| 166 | apm_->ProcessReverseStream(reverse_->channels(), reverse_config_, |
| 167 | reverse_config_, reverse_->channels())); |
| 168 | } |
| 169 | |
| 170 | void DebugDumpReplayer::OnConfigEvent(const audioproc::Config& msg) { |
| 171 | MaybeRecreateApm(msg); |
| 172 | ConfigureApm(msg); |
| 173 | } |
| 174 | |
Alex Loiko | 6234722 | 2018-09-10 10:18:07 +0200 | [diff] [blame] | 175 | void DebugDumpReplayer::OnRuntimeSettingEvent( |
| 176 | const audioproc::RuntimeSetting& msg) { |
| 177 | RTC_CHECK(apm_.get()); |
| 178 | ReplayRuntimeSetting(apm_.get(), msg); |
| 179 | } |
| 180 | |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 181 | void DebugDumpReplayer::MaybeRecreateApm(const audioproc::Config& msg) { |
| 182 | // These configurations cannot be changed on the fly. |
| 183 | Config config; |
| 184 | RTC_CHECK(msg.has_aec_delay_agnostic_enabled()); |
| 185 | config.Set<DelayAgnostic>( |
| 186 | new DelayAgnostic(msg.aec_delay_agnostic_enabled())); |
| 187 | |
| 188 | RTC_CHECK(msg.has_noise_robust_agc_enabled()); |
| 189 | config.Set<ExperimentalAgc>( |
| 190 | new ExperimentalAgc(msg.noise_robust_agc_enabled())); |
| 191 | |
| 192 | RTC_CHECK(msg.has_transient_suppression_enabled()); |
| 193 | config.Set<ExperimentalNs>( |
| 194 | new ExperimentalNs(msg.transient_suppression_enabled())); |
| 195 | |
| 196 | RTC_CHECK(msg.has_aec_extended_filter_enabled()); |
| 197 | config.Set<ExtendedFilter>( |
| 198 | new ExtendedFilter(msg.aec_extended_filter_enabled())); |
| 199 | |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 200 | // We only create APM once, since changes on these fields should not |
| 201 | // happen in current implementation. |
| 202 | if (!apm_.get()) { |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame] | 203 | apm_.reset(AudioProcessingBuilder().Create(config)); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 208 | AudioProcessing::Config apm_config; |
| 209 | |
Sam Zackrisson | b3b47ad | 2018-08-17 16:26:14 +0200 | [diff] [blame] | 210 | // AEC2/AECM configs. |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 211 | RTC_CHECK(msg.has_aec_enabled()); |
Sam Zackrisson | b3b47ad | 2018-08-17 16:26:14 +0200 | [diff] [blame] | 212 | RTC_CHECK(msg.has_aecm_enabled()); |
| 213 | apm_config.echo_canceller.enabled = msg.aec_enabled() || msg.aecm_enabled(); |
| 214 | apm_config.echo_canceller.mobile_mode = msg.aecm_enabled(); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 215 | |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 216 | RTC_CHECK(msg.has_aec_suppression_level()); |
Sam Zackrisson | cdf0e6d | 2018-09-17 11:05:17 +0200 | [diff] [blame] | 217 | apm_config.echo_canceller.legacy_moderate_suppression_level = |
saza | be490b2 | 2018-10-03 17:03:13 +0200 | [diff] [blame] | 218 | static_cast<EchoCancellationImpl::SuppressionLevel>( |
Sam Zackrisson | cdf0e6d | 2018-09-17 11:05:17 +0200 | [diff] [blame] | 219 | msg.aec_suppression_level()) == |
saza | be490b2 | 2018-10-03 17:03:13 +0200 | [diff] [blame] | 220 | EchoCancellationImpl::SuppressionLevel::kModerateSuppression; |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 221 | |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 222 | // AGC configs. |
| 223 | RTC_CHECK(msg.has_agc_enabled()); |
| 224 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 225 | apm_->gain_control()->Enable(msg.agc_enabled())); |
| 226 | |
| 227 | RTC_CHECK(msg.has_agc_mode()); |
| 228 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 229 | apm_->gain_control()->set_mode( |
| 230 | static_cast<GainControl::Mode>(msg.agc_mode()))); |
| 231 | |
| 232 | RTC_CHECK(msg.has_agc_limiter_enabled()); |
| 233 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 234 | apm_->gain_control()->enable_limiter(msg.agc_limiter_enabled())); |
| 235 | |
| 236 | // HPF configs. |
| 237 | RTC_CHECK(msg.has_hpf_enabled()); |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 238 | apm_config.high_pass_filter.enabled = msg.hpf_enabled(); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 239 | |
| 240 | // NS configs. |
| 241 | RTC_CHECK(msg.has_ns_enabled()); |
| 242 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 243 | apm_->noise_suppression()->Enable(msg.ns_enabled())); |
| 244 | |
| 245 | RTC_CHECK(msg.has_ns_level()); |
| 246 | RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 247 | apm_->noise_suppression()->set_level( |
| 248 | static_cast<NoiseSuppression::Level>(msg.ns_level()))); |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 249 | |
Alex Loiko | 6234722 | 2018-09-10 10:18:07 +0200 | [diff] [blame] | 250 | RTC_CHECK(msg.has_pre_amplifier_enabled()); |
| 251 | apm_config.pre_amplifier.enabled = msg.pre_amplifier_enabled(); |
| 252 | apm_config.pre_amplifier.fixed_gain_factor = |
| 253 | msg.pre_amplifier_fixed_gain_factor(); |
| 254 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 255 | apm_->ApplyConfig(apm_config); |
minyue | 0de1c13 | 2016-03-17 02:39:30 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void DebugDumpReplayer::LoadNextMessage() { |
| 259 | has_next_event_ = |
| 260 | debug_file_ && ReadMessageFromFile(debug_file_, &next_event_); |
| 261 | } |
| 262 | |
| 263 | } // namespace test |
| 264 | } // namespace webrtc |