niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 16 | extern "C" { |
| 17 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 18 | } |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/aec/echo_cancellation.h" |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | typedef void Handle; |
| 25 | |
| 26 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 27 | int16_t MapSetting(EchoCancellation::SuppressionLevel level) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | switch (level) { |
| 29 | case EchoCancellation::kLowSuppression: |
| 30 | return kAecNlpConservative; |
| 31 | case EchoCancellation::kModerateSuppression: |
| 32 | return kAecNlpModerate; |
| 33 | case EchoCancellation::kHighSuppression: |
| 34 | return kAecNlpAggressive; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 36 | assert(false); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 37 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 40 | AudioProcessing::Error MapError(int err) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | switch (err) { |
| 42 | case AEC_UNSUPPORTED_FUNCTION_ERROR: |
| 43 | return AudioProcessing::kUnsupportedFunctionError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | case AEC_BAD_PARAMETER_ERROR: |
| 45 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | case AEC_BAD_PARAMETER_WARNING: |
| 47 | return AudioProcessing::kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | default: |
| 49 | // AEC_UNSPECIFIED_ERROR |
| 50 | // AEC_UNINITIALIZED_ERROR |
| 51 | // AEC_NULL_POINTER_ERROR |
| 52 | return AudioProcessing::kUnspecifiedError; |
| 53 | } |
| 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 56 | // Maximum length that a frame of samples can have. |
| 57 | static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; |
| 58 | // Maximum number of frames to buffer in the render queue. |
| 59 | // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 60 | // reverse and forward call numbers. |
| 61 | static const size_t kMaxNumFramesToBuffer = 100; |
| 62 | } // namespace |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 63 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 64 | EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 65 | rtc::CriticalSection* crit_render, |
| 66 | rtc::CriticalSection* crit_capture) |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 67 | : ProcessingComponent(), |
| 68 | apm_(apm), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 69 | crit_render_(crit_render), |
| 70 | crit_capture_(crit_capture), |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 71 | drift_compensation_enabled_(false), |
| 72 | metrics_enabled_(false), |
| 73 | suppression_level_(kModerateSuppression), |
| 74 | stream_drift_samples_(0), |
| 75 | was_stream_drift_set_(false), |
| 76 | stream_has_echo_(false), |
| 77 | delay_logging_enabled_(false), |
| 78 | extended_filter_enabled_(false), |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 79 | delay_agnostic_enabled_(false), |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame^] | 80 | next_generation_aec_enabled_(false), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 81 | render_queue_element_max_size_(0) { |
| 82 | RTC_DCHECK(apm); |
| 83 | RTC_DCHECK(crit_render); |
| 84 | RTC_DCHECK(crit_capture); |
| 85 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
| 87 | EchoCancellationImpl::~EchoCancellationImpl() {} |
| 88 | |
| 89 | int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 90 | rtc::CritScope cs_render(crit_render_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 92 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 95 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | assert(audio->num_channels() == apm_->num_reverse_channels()); |
| 97 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 98 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
| 100 | // The ordering convention must be followed to pass to the correct AEC. |
| 101 | size_t handle_index = 0; |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 102 | render_queue_buffer_.clear(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 103 | for (size_t i = 0; i < apm_->num_output_channels(); i++) { |
| 104 | for (size_t j = 0; j < audio->num_channels(); j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 106 | // Retrieve any error code produced by the buffering of the farend |
| 107 | // signal |
| 108 | err = WebRtcAec_GetBufferFarendError( |
| 109 | my_handle, audio->split_bands_const_f(j)[kBand0To8kHz], |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 110 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 112 | if (err != AudioProcessing::kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 113 | return MapError(err); // TODO(ajm): warning possible? |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 116 | // Buffer the samples in the render queue. |
| 117 | render_queue_buffer_.insert(render_queue_buffer_.end(), |
| 118 | audio->split_bands_const_f(j)[kBand0To8kHz], |
| 119 | (audio->split_bands_const_f(j)[kBand0To8kHz] + |
| 120 | audio->num_frames_per_band())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 124 | // Insert the samples into the queue. |
| 125 | if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 126 | // The data queue is full and needs to be emptied. |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 127 | ReadQueuedRenderData(); |
| 128 | |
| 129 | // Retry the insert (should always work). |
| 130 | RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
| 131 | } |
| 132 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 133 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 136 | // Read chunks of data that were received and queued on the render side from |
| 137 | // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 138 | void EchoCancellationImpl::ReadQueuedRenderData() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 139 | rtc::CritScope cs_capture(crit_capture_); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 140 | if (!is_component_enabled()) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 145 | size_t handle_index = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 146 | size_t buffer_index = 0; |
| 147 | const size_t num_frames_per_band = |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 148 | capture_queue_buffer_.size() / |
| 149 | (apm_->num_output_channels() * apm_->num_reverse_channels()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 150 | for (size_t i = 0; i < apm_->num_output_channels(); i++) { |
| 151 | for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 152 | Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
| 153 | WebRtcAec_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], |
| 154 | num_frames_per_band); |
| 155 | |
| 156 | buffer_index += num_frames_per_band; |
| 157 | handle_index++; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 164 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 166 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (!apm_->was_stream_delay_set()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 170 | return AudioProcessing::kStreamParameterNotSetError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (drift_compensation_enabled_ && !was_stream_drift_set_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 174 | return AudioProcessing::kStreamParameterNotSetError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 177 | assert(audio->num_frames_per_band() <= 160); |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 178 | assert(audio->num_channels() == apm_->num_proc_channels()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 180 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | |
| 182 | // The ordering convention must be followed to pass to the correct AEC. |
| 183 | size_t handle_index = 0; |
| 184 | stream_has_echo_ = false; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 185 | for (size_t i = 0; i < audio->num_channels(); i++) { |
| 186 | for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | Handle* my_handle = handle(handle_index); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 188 | err = WebRtcAec_Process(my_handle, audio->split_bands_const_f(i), |
| 189 | audio->num_bands(), audio->split_bands_f(i), |
| 190 | audio->num_frames_per_band(), |
| 191 | apm_->stream_delay_ms(), stream_drift_samples_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 193 | if (err != AudioProcessing::kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 194 | err = MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | // TODO(ajm): Figure out how to return warnings properly. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 196 | if (err != AudioProcessing::kBadStreamParameterWarning) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | return err; |
| 198 | } |
| 199 | } |
| 200 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 201 | int status = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | err = WebRtcAec_get_echo_status(my_handle, &status); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 203 | if (err != AudioProcessing::kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 204 | return MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (status == 1) { |
| 208 | stream_has_echo_ = true; |
| 209 | } |
| 210 | |
| 211 | handle_index++; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | was_stream_drift_set_ = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 216 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int EchoCancellationImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 220 | // Run in a single-threaded manner. |
| 221 | rtc::CritScope cs_render(crit_render_); |
| 222 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | // Ensure AEC and AECM are not both enabled. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 224 | // The is_enabled call is safe from a deadlock perspective |
| 225 | // as both locks are already held in the correct order. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | if (enable && apm_->echo_control_mobile()->is_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 227 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return EnableComponent(enable); |
| 231 | } |
| 232 | |
| 233 | bool EchoCancellationImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 234 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | return is_component_enabled(); |
| 236 | } |
| 237 | |
| 238 | int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 239 | { |
| 240 | if (MapSetting(level) == -1) { |
| 241 | return AudioProcessing::kBadParameterError; |
| 242 | } |
| 243 | rtc::CritScope cs(crit_capture_); |
| 244 | suppression_level_ = level; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | return Configure(); |
| 247 | } |
| 248 | |
| 249 | EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level() |
| 250 | const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 251 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | return suppression_level_; |
| 253 | } |
| 254 | |
| 255 | int EchoCancellationImpl::enable_drift_compensation(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 256 | { |
| 257 | rtc::CritScope cs(crit_capture_); |
| 258 | drift_compensation_enabled_ = enable; |
| 259 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | return Configure(); |
| 261 | } |
| 262 | |
| 263 | bool EchoCancellationImpl::is_drift_compensation_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 264 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | return drift_compensation_enabled_; |
| 266 | } |
| 267 | |
andrew@webrtc.org | 6be1e93 | 2013-03-01 18:47:28 +0000 | [diff] [blame] | 268 | void EchoCancellationImpl::set_stream_drift_samples(int drift) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 269 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | was_stream_drift_set_ = true; |
| 271 | stream_drift_samples_ = drift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int EchoCancellationImpl::stream_drift_samples() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 275 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | return stream_drift_samples_; |
| 277 | } |
| 278 | |
| 279 | int EchoCancellationImpl::enable_metrics(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 280 | { |
| 281 | rtc::CritScope cs(crit_capture_); |
| 282 | metrics_enabled_ = enable; |
| 283 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 284 | return Configure(); |
| 285 | } |
| 286 | |
| 287 | bool EchoCancellationImpl::are_metrics_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 288 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | return metrics_enabled_; |
| 290 | } |
| 291 | |
| 292 | // TODO(ajm): we currently just use the metrics from the first AEC. Think more |
| 293 | // aboue the best way to extend this to multi-channel. |
| 294 | int EchoCancellationImpl::GetMetrics(Metrics* metrics) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 295 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 296 | if (metrics == NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 297 | return AudioProcessing::kNullPointerError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (!is_component_enabled() || !metrics_enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 301 | return AudioProcessing::kNotEnabledError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | AecMetrics my_metrics; |
| 305 | memset(&my_metrics, 0, sizeof(my_metrics)); |
| 306 | memset(metrics, 0, sizeof(Metrics)); |
| 307 | |
| 308 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 309 | int err = WebRtcAec_GetMetrics(my_handle, &my_metrics); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 310 | if (err != AudioProcessing::kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 311 | return MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant; |
| 315 | metrics->residual_echo_return_loss.average = my_metrics.rerl.average; |
| 316 | metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max; |
| 317 | metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min; |
| 318 | |
| 319 | metrics->echo_return_loss.instant = my_metrics.erl.instant; |
| 320 | metrics->echo_return_loss.average = my_metrics.erl.average; |
| 321 | metrics->echo_return_loss.maximum = my_metrics.erl.max; |
| 322 | metrics->echo_return_loss.minimum = my_metrics.erl.min; |
| 323 | |
| 324 | metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant; |
| 325 | metrics->echo_return_loss_enhancement.average = my_metrics.erle.average; |
| 326 | metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max; |
| 327 | metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min; |
| 328 | |
| 329 | metrics->a_nlp.instant = my_metrics.aNlp.instant; |
| 330 | metrics->a_nlp.average = my_metrics.aNlp.average; |
| 331 | metrics->a_nlp.maximum = my_metrics.aNlp.max; |
| 332 | metrics->a_nlp.minimum = my_metrics.aNlp.min; |
| 333 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 334 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool EchoCancellationImpl::stream_has_echo() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 338 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | return stream_has_echo_; |
| 340 | } |
| 341 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 342 | int EchoCancellationImpl::enable_delay_logging(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 343 | { |
| 344 | rtc::CritScope cs(crit_capture_); |
| 345 | delay_logging_enabled_ = enable; |
| 346 | } |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 347 | return Configure(); |
| 348 | } |
| 349 | |
| 350 | bool EchoCancellationImpl::is_delay_logging_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 351 | rtc::CritScope cs(crit_capture_); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 352 | return delay_logging_enabled_; |
| 353 | } |
| 354 | |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 355 | bool EchoCancellationImpl::is_delay_agnostic_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 356 | rtc::CritScope cs(crit_capture_); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 357 | return delay_agnostic_enabled_; |
| 358 | } |
| 359 | |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame^] | 360 | bool EchoCancellationImpl::is_next_generation_aec_enabled() const { |
| 361 | rtc::CritScope cs(crit_capture_); |
| 362 | return next_generation_aec_enabled_; |
| 363 | } |
| 364 | |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 365 | bool EchoCancellationImpl::is_extended_filter_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 366 | rtc::CritScope cs(crit_capture_); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 367 | return extended_filter_enabled_; |
| 368 | } |
| 369 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 370 | // TODO(bjornv): How should we handle the multi-channel case? |
| 371 | int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 372 | rtc::CritScope cs(crit_capture_); |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 373 | float fraction_poor_delays = 0; |
| 374 | return GetDelayMetrics(median, std, &fraction_poor_delays); |
| 375 | } |
| 376 | |
| 377 | int EchoCancellationImpl::GetDelayMetrics(int* median, int* std, |
| 378 | float* fraction_poor_delays) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 379 | rtc::CritScope cs(crit_capture_); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 380 | if (median == NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 381 | return AudioProcessing::kNullPointerError; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 382 | } |
| 383 | if (std == NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 384 | return AudioProcessing::kNullPointerError; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | if (!is_component_enabled() || !delay_logging_enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 388 | return AudioProcessing::kNotEnabledError; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 392 | const int err = |
| 393 | WebRtcAec_GetDelayMetrics(my_handle, median, std, fraction_poor_delays); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 394 | if (err != AudioProcessing::kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 395 | return MapError(err); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 396 | } |
| 397 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 398 | return AudioProcessing::kNoError; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 399 | } |
| 400 | |
bjornv@webrtc.org | 91d11b3 | 2013-03-05 16:53:09 +0000 | [diff] [blame] | 401 | struct AecCore* EchoCancellationImpl::aec_core() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 402 | rtc::CritScope cs(crit_capture_); |
bjornv@webrtc.org | 91d11b3 | 2013-03-05 16:53:09 +0000 | [diff] [blame] | 403 | if (!is_component_enabled()) { |
| 404 | return NULL; |
| 405 | } |
| 406 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 407 | return WebRtcAec_aec_core(my_handle); |
| 408 | } |
| 409 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | int EchoCancellationImpl::Initialize() { |
| 411 | int err = ProcessingComponent::Initialize(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 412 | { |
| 413 | rtc::CritScope cs(crit_capture_); |
| 414 | if (err != AudioProcessing::kNoError || !is_component_enabled()) { |
| 415 | return err; |
| 416 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 419 | AllocateRenderQueue(); |
| 420 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 421 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | } |
| 423 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 424 | void EchoCancellationImpl::AllocateRenderQueue() { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 425 | const size_t new_render_queue_element_max_size = std::max<size_t>( |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 426 | static_cast<size_t>(1), |
| 427 | kMaxAllowedValuesOfSamplesPerFrame * num_handles_required()); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 428 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 429 | rtc::CritScope cs_render(crit_render_); |
| 430 | rtc::CritScope cs_capture(crit_capture_); |
| 431 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 432 | // Reallocate the queue if the queue item size is too small to fit the |
| 433 | // data to put in the queue. |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 434 | if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 435 | render_queue_element_max_size_ = new_render_queue_element_max_size; |
| 436 | |
| 437 | std::vector<float> template_queue_element(render_queue_element_max_size_); |
| 438 | |
| 439 | render_signal_queue_.reset( |
| 440 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 441 | kMaxNumFramesToBuffer, template_queue_element, |
| 442 | RenderQueueItemVerifier<float>(render_queue_element_max_size_))); |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 443 | |
| 444 | render_queue_buffer_.resize(render_queue_element_max_size_); |
| 445 | capture_queue_buffer_.resize(render_queue_element_max_size_); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 446 | } else { |
| 447 | render_signal_queue_->Clear(); |
| 448 | } |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 449 | } |
| 450 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 451 | void EchoCancellationImpl::SetExtraOptions(const Config& config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 452 | { |
| 453 | rtc::CritScope cs(crit_capture_); |
| 454 | extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled; |
| 455 | delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled; |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame^] | 456 | next_generation_aec_enabled_ = config.Get<NextGenerationAec>().enabled; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 457 | } |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 458 | Configure(); |
| 459 | } |
| 460 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 461 | void* EchoCancellationImpl::CreateHandle() const { |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 462 | return WebRtcAec_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | } |
| 464 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 465 | void EchoCancellationImpl::DestroyHandle(void* handle) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | assert(handle != NULL); |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 467 | WebRtcAec_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int EchoCancellationImpl::InitializeHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 471 | // Not locked as it only relies on APM public API which is threadsafe. |
| 472 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | assert(handle != NULL); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 474 | // TODO(ajm): Drift compensation is disabled in practice. If restored, it |
| 475 | // should be managed internally and not depend on the hardware sample rate. |
| 476 | // For now, just hardcode a 48 kHz value. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | return WebRtcAec_Init(static_cast<Handle*>(handle), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 478 | apm_->proc_sample_rate_hz(), 48000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | int EchoCancellationImpl::ConfigureHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 482 | rtc::CritScope cs_render(crit_render_); |
| 483 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 484 | assert(handle != NULL); |
| 485 | AecConfig config; |
| 486 | config.metricsMode = metrics_enabled_; |
| 487 | config.nlpMode = MapSetting(suppression_level_); |
| 488 | config.skewMode = drift_compensation_enabled_; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 489 | config.delay_logging = delay_logging_enabled_; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 490 | WebRtcAec_enable_extended_filter( |
| 491 | WebRtcAec_aec_core(static_cast<Handle*>(handle)), |
| 492 | extended_filter_enabled_ ? 1 : 0); |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 493 | WebRtcAec_enable_delay_agnostic( |
| 494 | WebRtcAec_aec_core(static_cast<Handle*>(handle)), |
| 495 | delay_agnostic_enabled_ ? 1 : 0); |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame^] | 496 | WebRtcAec_enable_next_generation_aec( |
| 497 | WebRtcAec_aec_core(static_cast<Handle*>(handle)), |
| 498 | next_generation_aec_enabled_ ? 1 : 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 499 | return WebRtcAec_set_config(static_cast<Handle*>(handle), config); |
| 500 | } |
| 501 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 502 | size_t EchoCancellationImpl::num_handles_required() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 503 | // Not locked as it only relies on APM public API which is threadsafe. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 504 | return apm_->num_output_channels() * apm_->num_reverse_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | int EchoCancellationImpl::GetHandleError(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 508 | // Not locked as it does not rely on anything in the state. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 509 | assert(handle != NULL); |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 510 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | } |
| 512 | } // namespace webrtc |