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