niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_processing_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
peah | 103ac7e | 2017-04-12 05:40:55 -0700 | [diff] [blame] | 13 | #include <math.h> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 14 | #include <algorithm> |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 15 | #include <string> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "common_audio/audio_converter.h" |
| 18 | #include "common_audio/channel_buffer.h" |
| 19 | #include "common_audio/include/audio_util.h" |
| 20 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 21 | #include "modules/audio_processing/aec/aec_core.h" |
| 22 | #include "modules/audio_processing/aec3/echo_canceller3.h" |
| 23 | #include "modules/audio_processing/agc/agc_manager_direct.h" |
| 24 | #include "modules/audio_processing/agc2/gain_controller2.h" |
| 25 | #include "modules/audio_processing/audio_buffer.h" |
| 26 | #include "modules/audio_processing/beamformer/nonlinear_beamformer.h" |
| 27 | #include "modules/audio_processing/common.h" |
| 28 | #include "modules/audio_processing/echo_cancellation_impl.h" |
| 29 | #include "modules/audio_processing/echo_control_mobile_impl.h" |
| 30 | #include "modules/audio_processing/gain_control_for_experimental_agc.h" |
| 31 | #include "modules/audio_processing/gain_control_impl.h" |
| 32 | #include "rtc_base/checks.h" |
| 33 | #include "rtc_base/logging.h" |
| 34 | #include "rtc_base/platform_file.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 35 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "rtc_base/trace_event.h" |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 37 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 39 | #endif |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | #include "modules/audio_processing/level_controller/level_controller.h" |
| 41 | #include "modules/audio_processing/level_estimator_impl.h" |
| 42 | #include "modules/audio_processing/low_cut_filter.h" |
| 43 | #include "modules/audio_processing/noise_suppression_impl.h" |
| 44 | #include "modules/audio_processing/residual_echo_detector.h" |
| 45 | #include "modules/audio_processing/transient/transient_suppressor.h" |
| 46 | #include "modules/audio_processing/voice_detection_impl.h" |
| 47 | #include "modules/include/module_common_types.h" |
| 48 | #include "system_wrappers/include/file_wrapper.h" |
| 49 | #include "system_wrappers/include/metrics.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 50 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 51 | // Check to verify that the define for the intelligibility enhancer is properly |
| 52 | // set. |
| 53 | #if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \ |
| 54 | (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \ |
| 55 | WEBRTC_INTELLIGIBILITY_ENHANCER != 1) |
| 56 | #error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1" |
| 57 | #endif |
| 58 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 59 | #define RETURN_ON_ERR(expr) \ |
| 60 | do { \ |
| 61 | int err = (expr); \ |
| 62 | if (err != kNoError) { \ |
| 63 | return err; \ |
| 64 | } \ |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 65 | } while (0) |
| 66 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | namespace webrtc { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 68 | |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 69 | constexpr int AudioProcessing::kNativeSampleRatesHz[]; |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 70 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 71 | namespace { |
| 72 | |
| 73 | static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) { |
| 74 | switch (layout) { |
| 75 | case AudioProcessing::kMono: |
| 76 | case AudioProcessing::kStereo: |
| 77 | return false; |
| 78 | case AudioProcessing::kMonoAndKeyboard: |
| 79 | case AudioProcessing::kStereoAndKeyboard: |
| 80 | return true; |
| 81 | } |
| 82 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 83 | RTC_NOTREACHED(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 86 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 87 | bool SampleRateSupportsMultiBand(int sample_rate_hz) { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 88 | return sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 89 | sample_rate_hz == AudioProcessing::kSampleRate48kHz; |
| 90 | } |
| 91 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 92 | int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) { |
| 93 | #ifdef WEBRTC_ARCH_ARM_FAMILY |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 94 | constexpr int kMaxSplittingNativeProcessRate = |
| 95 | AudioProcessing::kSampleRate32kHz; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 96 | #else |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 97 | constexpr int kMaxSplittingNativeProcessRate = |
| 98 | AudioProcessing::kSampleRate48kHz; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 99 | #endif |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 100 | static_assert( |
| 101 | kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz, |
| 102 | ""); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 103 | const int uppermost_native_rate = band_splitting_required |
| 104 | ? kMaxSplittingNativeProcessRate |
| 105 | : AudioProcessing::kSampleRate48kHz; |
| 106 | |
| 107 | for (auto rate : AudioProcessing::kNativeSampleRatesHz) { |
| 108 | if (rate >= uppermost_native_rate) { |
| 109 | return uppermost_native_rate; |
| 110 | } |
| 111 | if (rate >= minimum_rate) { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 112 | return rate; |
| 113 | } |
| 114 | } |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 115 | RTC_NOTREACHED(); |
| 116 | return uppermost_native_rate; |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 117 | } |
| 118 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 119 | // Maximum lengths that frame of samples being passed from the render side to |
| 120 | // the capture side can have (does not apply to AEC3). |
| 121 | static const size_t kMaxAllowedValuesOfSamplesPerBand = 160; |
| 122 | static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480; |
| 123 | |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 124 | // Maximum number of frames to buffer in the render queue. |
| 125 | // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 126 | // reverse and forward call numbers. |
| 127 | static const size_t kMaxNumFramesToBuffer = 100; |
| 128 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 129 | class HighPassFilterImpl : public HighPassFilter { |
| 130 | public: |
| 131 | explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {} |
| 132 | ~HighPassFilterImpl() override = default; |
| 133 | |
| 134 | // HighPassFilter implementation. |
| 135 | int Enable(bool enable) override { |
| 136 | apm_->MutateConfig([enable](AudioProcessing::Config* config) { |
| 137 | config->high_pass_filter.enabled = enable; |
| 138 | }); |
| 139 | |
| 140 | return AudioProcessing::kNoError; |
| 141 | } |
| 142 | |
| 143 | bool is_enabled() const override { |
| 144 | return apm_->GetConfig().high_pass_filter.enabled; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | AudioProcessingImpl* apm_; |
| 149 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl); |
| 150 | }; |
| 151 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 152 | webrtc::InternalAPMStreamsConfig ToStreamsConfig( |
| 153 | const ProcessingConfig& api_format) { |
| 154 | webrtc::InternalAPMStreamsConfig result; |
| 155 | result.input_sample_rate = api_format.input_stream().sample_rate_hz(); |
| 156 | result.input_num_channels = api_format.input_stream().num_channels(); |
| 157 | result.output_num_channels = api_format.output_stream().num_channels(); |
| 158 | result.render_input_num_channels = |
| 159 | api_format.reverse_input_stream().num_channels(); |
| 160 | result.render_input_sample_rate = |
| 161 | api_format.reverse_input_stream().sample_rate_hz(); |
| 162 | result.output_sample_rate = api_format.output_stream().sample_rate_hz(); |
| 163 | result.render_output_sample_rate = |
| 164 | api_format.reverse_output_stream().sample_rate_hz(); |
| 165 | result.render_output_num_channels = |
| 166 | api_format.reverse_output_stream().num_channels(); |
| 167 | return result; |
| 168 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 169 | } // namespace |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 170 | |
| 171 | // Throughout webrtc, it's assumed that success is represented by zero. |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 172 | static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero"); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 173 | |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 174 | AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates( |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 175 | bool capture_post_processor_enabled, |
| 176 | bool render_pre_processor_enabled) |
| 177 | : capture_post_processor_enabled_(capture_post_processor_enabled), |
| 178 | render_pre_processor_enabled_(render_pre_processor_enabled) {} |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 179 | |
| 180 | bool AudioProcessingImpl::ApmSubmoduleStates::Update( |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 181 | bool low_cut_filter_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 182 | bool echo_canceller_enabled, |
| 183 | bool mobile_echo_controller_enabled, |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 184 | bool residual_echo_detector_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 185 | bool noise_suppressor_enabled, |
| 186 | bool intelligibility_enhancer_enabled, |
| 187 | bool beamformer_enabled, |
| 188 | bool adaptive_gain_controller_enabled, |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 189 | bool gain_controller2_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 190 | bool level_controller_enabled, |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 191 | bool echo_controller_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 192 | bool voice_activity_detector_enabled, |
| 193 | bool level_estimator_enabled, |
| 194 | bool transient_suppressor_enabled) { |
| 195 | bool changed = false; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 196 | changed |= (low_cut_filter_enabled != low_cut_filter_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 197 | changed |= (echo_canceller_enabled != echo_canceller_enabled_); |
| 198 | changed |= |
| 199 | (mobile_echo_controller_enabled != mobile_echo_controller_enabled_); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 200 | changed |= |
| 201 | (residual_echo_detector_enabled != residual_echo_detector_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 202 | changed |= (noise_suppressor_enabled != noise_suppressor_enabled_); |
| 203 | changed |= |
| 204 | (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_); |
| 205 | changed |= (beamformer_enabled != beamformer_enabled_); |
| 206 | changed |= |
| 207 | (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 208 | changed |= |
| 209 | (gain_controller2_enabled != gain_controller2_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 210 | changed |= (level_controller_enabled != level_controller_enabled_); |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 211 | changed |= (echo_controller_enabled != echo_controller_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 212 | changed |= (level_estimator_enabled != level_estimator_enabled_); |
| 213 | changed |= |
| 214 | (voice_activity_detector_enabled != voice_activity_detector_enabled_); |
| 215 | changed |= (transient_suppressor_enabled != transient_suppressor_enabled_); |
| 216 | if (changed) { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 217 | low_cut_filter_enabled_ = low_cut_filter_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 218 | echo_canceller_enabled_ = echo_canceller_enabled; |
| 219 | mobile_echo_controller_enabled_ = mobile_echo_controller_enabled; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 220 | residual_echo_detector_enabled_ = residual_echo_detector_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 221 | noise_suppressor_enabled_ = noise_suppressor_enabled; |
| 222 | intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled; |
| 223 | beamformer_enabled_ = beamformer_enabled; |
| 224 | adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 225 | gain_controller2_enabled_ = gain_controller2_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 226 | level_controller_enabled_ = level_controller_enabled; |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 227 | echo_controller_enabled_ = echo_controller_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 228 | level_estimator_enabled_ = level_estimator_enabled; |
| 229 | voice_activity_detector_enabled_ = voice_activity_detector_enabled; |
| 230 | transient_suppressor_enabled_ = transient_suppressor_enabled; |
| 231 | } |
| 232 | |
| 233 | changed |= first_update_; |
| 234 | first_update_ = false; |
| 235 | return changed; |
| 236 | } |
| 237 | |
| 238 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive() |
| 239 | const { |
| 240 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
| 241 | return CaptureMultiBandProcessingActive() || |
peah | 5277584 | 2017-05-16 06:14:09 -0700 | [diff] [blame] | 242 | intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 243 | #else |
peah | 5277584 | 2017-05-16 06:14:09 -0700 | [diff] [blame] | 244 | return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 245 | #endif |
| 246 | } |
| 247 | |
| 248 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive() |
| 249 | const { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 250 | return low_cut_filter_enabled_ || echo_canceller_enabled_ || |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 251 | mobile_echo_controller_enabled_ || noise_suppressor_enabled_ || |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 252 | beamformer_enabled_ || adaptive_gain_controller_enabled_ || |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 253 | echo_controller_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 254 | } |
| 255 | |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame] | 256 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive() |
| 257 | const { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 258 | return level_controller_enabled_ || gain_controller2_enabled_ || |
| 259 | capture_post_processor_enabled_; |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame] | 260 | } |
| 261 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 262 | bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive() |
| 263 | const { |
| 264 | return RenderMultiBandProcessingActive() || echo_canceller_enabled_ || |
ivoc | 20270be | 2016-11-15 05:24:35 -0800 | [diff] [blame] | 265 | mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ || |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 266 | echo_controller_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 269 | bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive() |
| 270 | const { |
| 271 | return render_pre_processor_enabled_; |
| 272 | } |
| 273 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 274 | bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive() |
| 275 | const { |
| 276 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
| 277 | return intelligibility_enhancer_enabled_; |
| 278 | #else |
| 279 | return false; |
| 280 | #endif |
| 281 | } |
| 282 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 283 | struct AudioProcessingImpl::ApmPublicSubmodules { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 284 | ApmPublicSubmodules() {} |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 285 | // Accessed externally of APM without any lock acquired. |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 286 | std::unique_ptr<EchoCancellationImpl> echo_cancellation; |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 287 | std::unique_ptr<EchoControlMobileImpl> echo_control_mobile; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 288 | std::unique_ptr<GainControlImpl> gain_control; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 289 | std::unique_ptr<LevelEstimatorImpl> level_estimator; |
| 290 | std::unique_ptr<NoiseSuppressionImpl> noise_suppression; |
| 291 | std::unique_ptr<VoiceDetectionImpl> voice_detection; |
| 292 | std::unique_ptr<GainControlForExperimentalAgc> |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 293 | gain_control_for_experimental_agc; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 294 | |
| 295 | // Accessed internally from both render and capture. |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 296 | std::unique_ptr<TransientSuppressor> transient_suppressor; |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 297 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 298 | std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer; |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 299 | #endif |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | struct AudioProcessingImpl::ApmPrivateSubmodules { |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 303 | ApmPrivateSubmodules(NonlinearBeamformer* beamformer, |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 304 | std::unique_ptr<CustomProcessing> capture_post_processor, |
| 305 | std::unique_ptr<CustomProcessing> render_pre_processor) |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 306 | : beamformer(beamformer), |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 307 | capture_post_processor(std::move(capture_post_processor)), |
| 308 | render_pre_processor(std::move(render_pre_processor)) {} |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 309 | // Accessed internally from capture or during initialization |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 310 | std::unique_ptr<NonlinearBeamformer> beamformer; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 311 | std::unique_ptr<AgcManagerDirect> agc_manager; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 312 | std::unique_ptr<GainController2> gain_controller2; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 313 | std::unique_ptr<LowCutFilter> low_cut_filter; |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 314 | std::unique_ptr<LevelController> level_controller; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 315 | std::unique_ptr<ResidualEchoDetector> residual_echo_detector; |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 316 | std::unique_ptr<EchoControl> echo_controller; |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 317 | std::unique_ptr<CustomProcessing> capture_post_processor; |
| 318 | std::unique_ptr<CustomProcessing> render_pre_processor; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 321 | AudioProcessing* AudioProcessing::Create() { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 322 | webrtc::Config config; |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 323 | return Create(config, nullptr, nullptr, nullptr, nullptr); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 324 | } |
| 325 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 326 | AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) { |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 327 | return Create(config, nullptr, nullptr, nullptr, nullptr); |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 328 | } |
| 329 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 330 | AudioProcessing* AudioProcessing::Create(const webrtc::Config& config, |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 331 | NonlinearBeamformer* beamformer) { |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 332 | return Create(config, nullptr, nullptr, nullptr, beamformer); |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | AudioProcessing* AudioProcessing::Create( |
| 336 | const webrtc::Config& config, |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 337 | std::unique_ptr<CustomProcessing> capture_post_processor, |
| 338 | std::unique_ptr<EchoControlFactory> echo_control_factory, |
| 339 | NonlinearBeamformer* beamformer) { |
| 340 | return Create(config, std::move(capture_post_processor), nullptr, |
| 341 | std::move(echo_control_factory), beamformer); |
| 342 | } |
| 343 | |
| 344 | AudioProcessing* AudioProcessing::Create( |
| 345 | const webrtc::Config& config, |
| 346 | std::unique_ptr<CustomProcessing> capture_post_processor, |
| 347 | std::unique_ptr<CustomProcessing> render_pre_processor, |
Gustaf Ullberg | 002ef28 | 2017-10-12 15:13:17 +0200 | [diff] [blame] | 348 | std::unique_ptr<EchoControlFactory> echo_control_factory, |
Gustaf Ullberg | d8579e0 | 2017-10-11 16:29:02 +0200 | [diff] [blame] | 349 | NonlinearBeamformer* beamformer) { |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 350 | AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>( |
Gustaf Ullberg | 002ef28 | 2017-10-12 15:13:17 +0200 | [diff] [blame] | 351 | config, std::move(capture_post_processor), |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 352 | std::move(render_pre_processor), std::move(echo_control_factory), |
| 353 | beamformer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | if (apm->Initialize() != kNoError) { |
| 355 | delete apm; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 356 | apm = nullptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return apm; |
| 360 | } |
| 361 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 362 | AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config) |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 363 | : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr) {} |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 364 | |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 365 | AudioProcessingImpl::AudioProcessingImpl( |
| 366 | const webrtc::Config& config, |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 367 | std::unique_ptr<CustomProcessing> capture_post_processor, |
| 368 | std::unique_ptr<CustomProcessing> render_pre_processor, |
Gustaf Ullberg | 002ef28 | 2017-10-12 15:13:17 +0200 | [diff] [blame] | 369 | std::unique_ptr<EchoControlFactory> echo_control_factory, |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 370 | NonlinearBeamformer* beamformer) |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 371 | : high_pass_filter_impl_(new HighPassFilterImpl(this)), |
Gustaf Ullberg | 002ef28 | 2017-10-12 15:13:17 +0200 | [diff] [blame] | 372 | echo_control_factory_(std::move(echo_control_factory)), |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 373 | submodule_states_(!!capture_post_processor, !!render_pre_processor), |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 374 | public_submodules_(new ApmPublicSubmodules()), |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 375 | private_submodules_( |
| 376 | new ApmPrivateSubmodules(beamformer, |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 377 | std::move(capture_post_processor), |
| 378 | std::move(render_pre_processor))), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 379 | constants_(config.Get<ExperimentalAgc>().startup_min_volume, |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 380 | config.Get<ExperimentalAgc>().clipped_level_min, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 381 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 382 | false), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 383 | #else |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 384 | config.Get<ExperimentalAgc>().enabled), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 385 | #endif |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 386 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 387 | capture_(false, |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 388 | #else |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 389 | capture_(config.Get<ExperimentalNs>().enabled, |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 390 | #endif |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 391 | config.Get<Beamforming>().array_geometry, |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 392 | config.Get<Beamforming>().target_direction), |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 393 | capture_nonlocked_(config.Get<Beamforming>().enabled, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 394 | config.Get<Intelligibility>().enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 395 | { |
| 396 | rtc::CritScope cs_render(&crit_render_); |
| 397 | rtc::CritScope cs_capture(&crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 398 | |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 399 | // Mark Echo Controller enabled if a factory is injected. |
| 400 | capture_nonlocked_.echo_controller_enabled = |
| 401 | static_cast<bool>(echo_control_factory_); |
| 402 | |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 403 | public_submodules_->echo_cancellation.reset( |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 404 | new EchoCancellationImpl(&crit_render_, &crit_capture_)); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 405 | public_submodules_->echo_control_mobile.reset( |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 406 | new EchoControlMobileImpl(&crit_render_, &crit_capture_)); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 407 | public_submodules_->gain_control.reset( |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 408 | new GainControlImpl(&crit_capture_, &crit_capture_)); |
solenberg | 949028f | 2015-12-15 11:39:38 -0800 | [diff] [blame] | 409 | public_submodules_->level_estimator.reset( |
| 410 | new LevelEstimatorImpl(&crit_capture_)); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 411 | public_submodules_->noise_suppression.reset( |
| 412 | new NoiseSuppressionImpl(&crit_capture_)); |
solenberg | a29386c | 2015-12-16 03:31:12 -0800 | [diff] [blame] | 413 | public_submodules_->voice_detection.reset( |
| 414 | new VoiceDetectionImpl(&crit_capture_)); |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 415 | public_submodules_->gain_control_for_experimental_agc.reset( |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 416 | new GainControlForExperimentalAgc( |
| 417 | public_submodules_->gain_control.get(), &crit_capture_)); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 418 | private_submodules_->residual_echo_detector.reset( |
| 419 | new ResidualEchoDetector()); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 420 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 421 | // TODO(peah): Move this creation to happen only when the level controller |
| 422 | // is enabled. |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 423 | private_submodules_->level_controller.reset(new LevelController()); |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 424 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 425 | // TODO(alessiob): Move the injected gain controller once injection is |
| 426 | // implemented. |
| 427 | private_submodules_->gain_controller2.reset(new GainController2()); |
| 428 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 429 | RTC_LOG(LS_INFO) << "Capture post processor activated: " |
| 430 | << !!private_submodules_->capture_post_processor; |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 431 | |
| 432 | RTC_LOG(LS_INFO) << "Render pre processor activated: " |
| 433 | << !!private_submodules_->render_pre_processor; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 434 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 435 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 436 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | AudioProcessingImpl::~AudioProcessingImpl() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 440 | // Depends on gain_control_ and |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 441 | // public_submodules_->gain_control_for_experimental_agc. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 442 | private_submodules_->agc_manager.reset(); |
| 443 | // Depends on gain_control_. |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 444 | public_submodules_->gain_control_for_experimental_agc.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 445 | } |
| 446 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 447 | int AudioProcessingImpl::Initialize() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 448 | // Run in a single-threaded manner during initialization. |
| 449 | rtc::CritScope cs_render(&crit_render_); |
| 450 | rtc::CritScope cs_capture(&crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | return InitializeLocked(); |
| 452 | } |
| 453 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 454 | int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz, |
| 455 | int capture_output_sample_rate_hz, |
| 456 | int render_input_sample_rate_hz, |
| 457 | ChannelLayout capture_input_layout, |
| 458 | ChannelLayout capture_output_layout, |
| 459 | ChannelLayout render_input_layout) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 460 | const ProcessingConfig processing_config = { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 461 | {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout), |
| 462 | LayoutHasKeyboard(capture_input_layout)}, |
| 463 | {capture_output_sample_rate_hz, |
| 464 | ChannelsFromLayout(capture_output_layout), |
| 465 | LayoutHasKeyboard(capture_output_layout)}, |
| 466 | {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout), |
| 467 | LayoutHasKeyboard(render_input_layout)}, |
| 468 | {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout), |
| 469 | LayoutHasKeyboard(render_input_layout)}}}; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 470 | |
| 471 | return Initialize(processing_config); |
| 472 | } |
| 473 | |
| 474 | int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 475 | // Run in a single-threaded manner during initialization. |
| 476 | rtc::CritScope cs_render(&crit_render_); |
| 477 | rtc::CritScope cs_capture(&crit_capture_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 478 | return InitializeLocked(processing_config); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 479 | } |
| 480 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 481 | int AudioProcessingImpl::MaybeInitializeRender( |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 482 | const ProcessingConfig& processing_config) { |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 483 | return MaybeInitialize(processing_config, false); |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 484 | } |
| 485 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 486 | int AudioProcessingImpl::MaybeInitializeCapture( |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 487 | const ProcessingConfig& processing_config, |
| 488 | bool force_initialization) { |
| 489 | return MaybeInitialize(processing_config, force_initialization); |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 490 | } |
| 491 | |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 492 | // Calls InitializeLocked() if any of the audio parameters have changed from |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 493 | // their current values (needs to be called while holding the crit_render_lock). |
| 494 | int AudioProcessingImpl::MaybeInitialize( |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 495 | const ProcessingConfig& processing_config, |
| 496 | bool force_initialization) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 497 | // Called from both threads. Thread check is therefore not possible. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 498 | if (processing_config == formats_.api_format && !force_initialization) { |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 499 | return kNoError; |
| 500 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 501 | |
| 502 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 503 | return InitializeLocked(processing_config); |
| 504 | } |
| 505 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 506 | int AudioProcessingImpl::InitializeLocked() { |
Per Ã…hgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 507 | UpdateActiveSubmoduleStates(); |
| 508 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 509 | const int capture_audiobuffer_num_channels = |
| 510 | capture_nonlocked_.beamformer_enabled |
| 511 | ? formats_.api_format.input_stream().num_channels() |
| 512 | : formats_.api_format.output_stream().num_channels(); |
| 513 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 514 | const int render_audiobuffer_num_output_frames = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 515 | formats_.api_format.reverse_output_stream().num_frames() == 0 |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 516 | ? formats_.render_processing_format.num_frames() |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 517 | : formats_.api_format.reverse_output_stream().num_frames(); |
| 518 | if (formats_.api_format.reverse_input_stream().num_channels() > 0) { |
| 519 | render_.render_audio.reset(new AudioBuffer( |
| 520 | formats_.api_format.reverse_input_stream().num_frames(), |
| 521 | formats_.api_format.reverse_input_stream().num_channels(), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 522 | formats_.render_processing_format.num_frames(), |
| 523 | formats_.render_processing_format.num_channels(), |
| 524 | render_audiobuffer_num_output_frames)); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 525 | if (formats_.api_format.reverse_input_stream() != |
| 526 | formats_.api_format.reverse_output_stream()) { |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 527 | render_.render_converter = AudioConverter::Create( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 528 | formats_.api_format.reverse_input_stream().num_channels(), |
| 529 | formats_.api_format.reverse_input_stream().num_frames(), |
| 530 | formats_.api_format.reverse_output_stream().num_channels(), |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 531 | formats_.api_format.reverse_output_stream().num_frames()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 532 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 533 | render_.render_converter.reset(nullptr); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 534 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 535 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 536 | render_.render_audio.reset(nullptr); |
| 537 | render_.render_converter.reset(nullptr); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 538 | } |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 539 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 540 | capture_.capture_audio.reset( |
| 541 | new AudioBuffer(formats_.api_format.input_stream().num_frames(), |
| 542 | formats_.api_format.input_stream().num_channels(), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 543 | capture_nonlocked_.capture_processing_format.num_frames(), |
| 544 | capture_audiobuffer_num_channels, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 545 | formats_.api_format.output_stream().num_frames())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 546 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 547 | public_submodules_->echo_cancellation->Initialize( |
| 548 | proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(), |
| 549 | num_proc_channels()); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 550 | AllocateRenderQueue(); |
| 551 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 552 | int success = public_submodules_->echo_cancellation->enable_metrics(true); |
| 553 | RTC_DCHECK_EQ(0, success); |
| 554 | success = public_submodules_->echo_cancellation->enable_delay_logging(true); |
| 555 | RTC_DCHECK_EQ(0, success); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 556 | public_submodules_->echo_control_mobile->Initialize( |
| 557 | proc_split_sample_rate_hz(), num_reverse_channels(), |
| 558 | num_output_channels()); |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 559 | |
| 560 | public_submodules_->gain_control->Initialize(num_proc_channels(), |
| 561 | proc_sample_rate_hz()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 562 | if (constants_.use_experimental_agc) { |
| 563 | if (!private_submodules_->agc_manager.get()) { |
| 564 | private_submodules_->agc_manager.reset(new AgcManagerDirect( |
| 565 | public_submodules_->gain_control.get(), |
| 566 | public_submodules_->gain_control_for_experimental_agc.get(), |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 567 | constants_.agc_startup_min_volume, constants_.agc_clipped_level_min)); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 568 | } |
| 569 | private_submodules_->agc_manager->Initialize(); |
| 570 | private_submodules_->agc_manager->SetCaptureMuted( |
| 571 | capture_.output_will_be_muted); |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 572 | public_submodules_->gain_control_for_experimental_agc->Initialize(); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 573 | } |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 574 | InitializeTransient(); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 575 | InitializeBeamformer(); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 576 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 577 | InitializeIntelligibility(); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 578 | #endif |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 579 | InitializeLowCutFilter(); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 580 | public_submodules_->noise_suppression->Initialize(num_proc_channels(), |
| 581 | proc_sample_rate_hz()); |
| 582 | public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz()); |
| 583 | public_submodules_->level_estimator->Initialize(); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 584 | InitializeLevelController(); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 585 | InitializeResidualEchoDetector(); |
Gustaf Ullberg | 8eb9c7d | 2017-10-14 08:28:46 +0200 | [diff] [blame] | 586 | InitializeEchoController(); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 587 | InitializeGainController2(); |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 588 | InitializePostProcessor(); |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 589 | InitializePreProcessor(); |
solenberg | 70f9903 | 2015-12-08 11:07:32 -0800 | [diff] [blame] | 590 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 591 | if (aec_dump_) { |
| 592 | aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format)); |
| 593 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 594 | return kNoError; |
| 595 | } |
| 596 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 597 | int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) { |
Per Ã…hgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 598 | UpdateActiveSubmoduleStates(); |
| 599 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 600 | for (const auto& stream : config.streams) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 601 | if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) { |
| 602 | return kBadSampleRateError; |
| 603 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 604 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 605 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 606 | const size_t num_in_channels = config.input_stream().num_channels(); |
| 607 | const size_t num_out_channels = config.output_stream().num_channels(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 608 | |
| 609 | // Need at least one input channel. |
| 610 | // Need either one output channel or as many outputs as there are inputs. |
| 611 | if (num_in_channels == 0 || |
| 612 | !(num_out_channels == 1 || num_out_channels == num_in_channels)) { |
Michael Graczyk | c204754 | 2015-07-22 21:06:11 -0700 | [diff] [blame] | 613 | return kBadNumberChannelsError; |
| 614 | } |
| 615 | |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 616 | if (capture_nonlocked_.beamformer_enabled && |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 617 | num_in_channels != capture_.array_geometry.size()) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 618 | return kBadNumberChannelsError; |
| 619 | } |
| 620 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 621 | formats_.api_format = config; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 622 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 623 | int capture_processing_rate = FindNativeProcessRateToUse( |
peah | 423d236 | 2016-04-09 16:06:52 -0700 | [diff] [blame] | 624 | std::min(formats_.api_format.input_stream().sample_rate_hz(), |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 625 | formats_.api_format.output_stream().sample_rate_hz()), |
| 626 | submodule_states_.CaptureMultiBandSubModulesActive() || |
| 627 | submodule_states_.RenderMultiBandSubModulesActive()); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 628 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 629 | capture_nonlocked_.capture_processing_format = |
| 630 | StreamConfig(capture_processing_rate); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 631 | |
peah | 2ce640f | 2017-04-07 03:57:48 -0700 | [diff] [blame] | 632 | int render_processing_rate; |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 633 | if (!capture_nonlocked_.echo_controller_enabled) { |
peah | 2ce640f | 2017-04-07 03:57:48 -0700 | [diff] [blame] | 634 | render_processing_rate = FindNativeProcessRateToUse( |
| 635 | std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(), |
| 636 | formats_.api_format.reverse_output_stream().sample_rate_hz()), |
| 637 | submodule_states_.CaptureMultiBandSubModulesActive() || |
| 638 | submodule_states_.RenderMultiBandSubModulesActive()); |
| 639 | } else { |
| 640 | render_processing_rate = capture_processing_rate; |
| 641 | } |
| 642 | |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 643 | // TODO(aluebs): Remove this restriction once we figure out why the 3-band |
| 644 | // splitting filter degrades the AEC performance. |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 645 | if (render_processing_rate > kSampleRate32kHz && |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 646 | !capture_nonlocked_.echo_controller_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 647 | render_processing_rate = submodule_states_.RenderMultiBandProcessingActive() |
| 648 | ? kSampleRate32kHz |
| 649 | : kSampleRate16kHz; |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 650 | } |
peah | 2ce640f | 2017-04-07 03:57:48 -0700 | [diff] [blame] | 651 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 652 | // If the forward sample rate is 8 kHz, the render stream is also processed |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 653 | // at this rate. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 654 | if (capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 655 | kSampleRate8kHz) { |
| 656 | render_processing_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 657 | } else { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 658 | render_processing_rate = |
| 659 | std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 660 | } |
| 661 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 662 | // Always downmix the render stream to mono for analysis. This has been |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 663 | // demonstrated to work well for AEC in most practical scenarios. |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 664 | if (submodule_states_.RenderMultiBandSubModulesActive()) { |
| 665 | formats_.render_processing_format = StreamConfig(render_processing_rate, 1); |
| 666 | } else { |
| 667 | formats_.render_processing_format = StreamConfig( |
| 668 | formats_.api_format.reverse_input_stream().sample_rate_hz(), |
| 669 | formats_.api_format.reverse_input_stream().num_channels()); |
| 670 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 671 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 672 | if (capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 673 | kSampleRate32kHz || |
| 674 | capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 675 | kSampleRate48kHz) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 676 | capture_nonlocked_.split_rate = kSampleRate16kHz; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 677 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 678 | capture_nonlocked_.split_rate = |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 679 | capture_nonlocked_.capture_processing_format.sample_rate_hz(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | return InitializeLocked(); |
| 683 | } |
| 684 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 685 | void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) { |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 686 | config_ = config; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 687 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 688 | bool config_ok = LevelController::Validate(config_.level_controller); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 689 | if (!config_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 690 | RTC_LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl |
| 691 | << "level_controller: " |
| 692 | << LevelController::ToString(config_.level_controller) |
| 693 | << std::endl |
| 694 | << "Reverting to default parameter set"; |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 695 | config_.level_controller = AudioProcessing::Config::LevelController(); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | // Run in a single-threaded manner when applying the settings. |
| 699 | rtc::CritScope cs_render(&crit_render_); |
| 700 | rtc::CritScope cs_capture(&crit_capture_); |
| 701 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 702 | // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled |
| 703 | // with the value in config_ everywhere in the code. |
| 704 | if (capture_nonlocked_.level_controller_enabled != |
| 705 | config_.level_controller.enabled) { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 706 | capture_nonlocked_.level_controller_enabled = |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 707 | config_.level_controller.enabled; |
| 708 | // TODO(peah): Remove the conditional initialization to always initialize |
| 709 | // the level controller regardless of whether it is enabled or not. |
| 710 | InitializeLevelController(); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 711 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 712 | RTC_LOG(LS_INFO) << "Level controller activated: " |
| 713 | << capture_nonlocked_.level_controller_enabled; |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 714 | |
| 715 | private_submodules_->level_controller->ApplyConfig(config_.level_controller); |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 716 | |
| 717 | InitializeLowCutFilter(); |
| 718 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 719 | RTC_LOG(LS_INFO) << "Highpass filter activated: " |
| 720 | << config_.high_pass_filter.enabled; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 721 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 722 | // Deprecated way of activating AEC3. |
| 723 | // TODO(gustaf): Remove when possible. |
Gustaf Ullberg | 8eb9c7d | 2017-10-14 08:28:46 +0200 | [diff] [blame] | 724 | if (config.echo_canceller3.enabled && !echo_control_factory_) { |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 725 | capture_nonlocked_.echo_controller_enabled = |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 726 | config_.echo_canceller3.enabled; |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 727 | echo_control_factory_ = |
| 728 | std::unique_ptr<EchoControlFactory>(new EchoCanceller3Factory()); |
Gustaf Ullberg | 8eb9c7d | 2017-10-14 08:28:46 +0200 | [diff] [blame] | 729 | InitializeEchoController(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 730 | RTC_LOG(LS_INFO) << "Echo canceller 3 activated: " |
| 731 | << capture_nonlocked_.echo_controller_enabled; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 732 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 733 | |
| 734 | config_ok = GainController2::Validate(config_.gain_controller2); |
| 735 | if (!config_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 736 | RTC_LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl |
| 737 | << "Gain Controller 2: " |
| 738 | << GainController2::ToString(config_.gain_controller2) |
| 739 | << std::endl |
| 740 | << "Reverting to default parameter set"; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 741 | config_.gain_controller2 = AudioProcessing::Config::GainController2(); |
| 742 | } |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 743 | InitializeGainController2(); |
| 744 | private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 745 | RTC_LOG(LS_INFO) << "Gain Controller 2 activated: " |
| 746 | << config_.gain_controller2.enabled; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 750 | // Run in a single-threaded manner when setting the extra options. |
| 751 | rtc::CritScope cs_render(&crit_render_); |
| 752 | rtc::CritScope cs_capture(&crit_capture_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 753 | |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 754 | public_submodules_->echo_cancellation->SetExtraOptions(config); |
| 755 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 756 | if (capture_.transient_suppressor_enabled != |
| 757 | config.Get<ExperimentalNs>().enabled) { |
| 758 | capture_.transient_suppressor_enabled = |
| 759 | config.Get<ExperimentalNs>().enabled; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 760 | InitializeTransient(); |
| 761 | } |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 762 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 763 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 764 | if (capture_nonlocked_.intelligibility_enabled != |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 765 | config.Get<Intelligibility>().enabled) { |
| 766 | capture_nonlocked_.intelligibility_enabled = |
| 767 | config.Get<Intelligibility>().enabled; |
| 768 | InitializeIntelligibility(); |
| 769 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 770 | #endif |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 771 | |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 772 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 773 | if (capture_nonlocked_.beamformer_enabled != |
| 774 | config.Get<Beamforming>().enabled) { |
| 775 | capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled; |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 776 | if (config.Get<Beamforming>().array_geometry.size() > 1) { |
| 777 | capture_.array_geometry = config.Get<Beamforming>().array_geometry; |
| 778 | } |
| 779 | capture_.target_direction = config.Get<Beamforming>().target_direction; |
| 780 | InitializeBeamformer(); |
| 781 | } |
| 782 | #endif // WEBRTC_ANDROID_PLATFORM_BUILD |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 783 | } |
| 784 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 785 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 786 | // Used as callback from submodules, hence locking is not allowed. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 787 | return capture_nonlocked_.capture_processing_format.sample_rate_hz(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 788 | } |
| 789 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 790 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 791 | // Used as callback from submodules, hence locking is not allowed. |
| 792 | return capture_nonlocked_.split_rate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 795 | size_t AudioProcessingImpl::num_reverse_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 796 | // Used as callback from submodules, hence locking is not allowed. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 797 | return formats_.render_processing_format.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 800 | size_t AudioProcessingImpl::num_input_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 801 | // Used as callback from submodules, hence locking is not allowed. |
| 802 | return formats_.api_format.input_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 805 | size_t AudioProcessingImpl::num_proc_channels() const { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 806 | // Used as callback from submodules, hence locking is not allowed. |
peah | edddac5 | 2017-05-16 01:08:58 -0700 | [diff] [blame] | 807 | return (capture_nonlocked_.beamformer_enabled || |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 808 | capture_nonlocked_.echo_controller_enabled) |
peah | edddac5 | 2017-05-16 01:08:58 -0700 | [diff] [blame] | 809 | ? 1 |
| 810 | : num_output_channels(); |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 813 | size_t AudioProcessingImpl::num_output_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 814 | // Used as callback from submodules, hence locking is not allowed. |
| 815 | return formats_.api_format.output_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 816 | } |
| 817 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 818 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 819 | rtc::CritScope cs(&crit_capture_); |
| 820 | capture_.output_will_be_muted = muted; |
| 821 | if (private_submodules_->agc_manager.get()) { |
| 822 | private_submodules_->agc_manager->SetCaptureMuted( |
| 823 | capture_.output_will_be_muted); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 824 | } |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 825 | } |
| 826 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 827 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 828 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 829 | size_t samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 830 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 831 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 832 | int output_sample_rate_hz, |
| 833 | ChannelLayout output_layout, |
| 834 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 835 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 836 | StreamConfig input_stream; |
| 837 | StreamConfig output_stream; |
| 838 | { |
| 839 | // Access the formats_.api_format.input_stream beneath the capture lock. |
| 840 | // The lock must be released as it is later required in the call |
| 841 | // to ProcessStream(,,,); |
| 842 | rtc::CritScope cs(&crit_capture_); |
| 843 | input_stream = formats_.api_format.input_stream(); |
| 844 | output_stream = formats_.api_format.output_stream(); |
| 845 | } |
| 846 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 847 | input_stream.set_sample_rate_hz(input_sample_rate_hz); |
| 848 | input_stream.set_num_channels(ChannelsFromLayout(input_layout)); |
| 849 | input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout)); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 850 | output_stream.set_sample_rate_hz(output_sample_rate_hz); |
| 851 | output_stream.set_num_channels(ChannelsFromLayout(output_layout)); |
| 852 | output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout)); |
| 853 | |
| 854 | if (samples_per_channel != input_stream.num_frames()) { |
| 855 | return kBadDataLengthError; |
| 856 | } |
| 857 | return ProcessStream(src, input_stream, output_stream, dest); |
| 858 | } |
| 859 | |
| 860 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
| 861 | const StreamConfig& input_config, |
| 862 | const StreamConfig& output_config, |
| 863 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 864 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 865 | ProcessingConfig processing_config; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 866 | bool reinitialization_required = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 867 | { |
| 868 | // Acquire the capture lock in order to safely call the function |
| 869 | // that retrieves the render side data. This function accesses apm |
| 870 | // getters that need the capture lock held when being called. |
| 871 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 872 | EmptyQueuedRenderAudio(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 873 | |
| 874 | if (!src || !dest) { |
| 875 | return kNullPointerError; |
| 876 | } |
| 877 | |
| 878 | processing_config = formats_.api_format; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 879 | reinitialization_required = UpdateActiveSubmoduleStates(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 880 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 881 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 882 | processing_config.input_stream() = input_config; |
| 883 | processing_config.output_stream() = output_config; |
| 884 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 885 | { |
| 886 | // Do conditional reinitialization. |
| 887 | rtc::CritScope cs_render(&crit_render_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 888 | RETURN_ON_ERR( |
| 889 | MaybeInitializeCapture(processing_config, reinitialization_required)); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 890 | } |
| 891 | rtc::CritScope cs_capture(&crit_capture_); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 892 | RTC_DCHECK_EQ(processing_config.input_stream().num_frames(), |
| 893 | formats_.api_format.input_stream().num_frames()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 894 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 895 | if (aec_dump_) { |
| 896 | RecordUnprocessedCaptureStream(src); |
| 897 | } |
| 898 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 899 | capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 900 | RETURN_ON_ERR(ProcessCaptureStreamLocked()); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 901 | capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 902 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 903 | if (aec_dump_) { |
| 904 | RecordProcessedCaptureStream(dest); |
| 905 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 906 | return kNoError; |
| 907 | } |
| 908 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 909 | void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 910 | EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(), |
| 911 | num_reverse_channels(), |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 912 | &aec_render_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 913 | |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 914 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 915 | |
| 916 | // Insert the samples into the queue. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 917 | if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 918 | // The data queue is full and needs to be emptied. |
| 919 | EmptyQueuedRenderAudio(); |
| 920 | |
| 921 | // Retry the insert (should always work). |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 922 | bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 923 | RTC_DCHECK(result); |
| 924 | } |
| 925 | |
| 926 | EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(), |
| 927 | num_reverse_channels(), |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 928 | &aecm_render_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 929 | |
| 930 | // Insert the samples into the queue. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 931 | if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) { |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 932 | // The data queue is full and needs to be emptied. |
| 933 | EmptyQueuedRenderAudio(); |
| 934 | |
| 935 | // Retry the insert (should always work). |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 936 | bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 937 | RTC_DCHECK(result); |
| 938 | } |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 939 | |
| 940 | if (!constants_.use_experimental_agc) { |
| 941 | GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_); |
| 942 | // Insert the samples into the queue. |
| 943 | if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) { |
| 944 | // The data queue is full and needs to be emptied. |
| 945 | EmptyQueuedRenderAudio(); |
| 946 | |
| 947 | // Retry the insert (should always work). |
| 948 | bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_); |
| 949 | RTC_DCHECK(result); |
| 950 | } |
| 951 | } |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 952 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 953 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 954 | void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) { |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 955 | ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_); |
| 956 | |
| 957 | // Insert the samples into the queue. |
| 958 | if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) { |
| 959 | // The data queue is full and needs to be emptied. |
| 960 | EmptyQueuedRenderAudio(); |
| 961 | |
| 962 | // Retry the insert (should always work). |
| 963 | bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_); |
| 964 | RTC_DCHECK(result); |
| 965 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | void AudioProcessingImpl::AllocateRenderQueue() { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 969 | const size_t new_aec_render_queue_element_max_size = |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 970 | std::max(static_cast<size_t>(1), |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 971 | kMaxAllowedValuesOfSamplesPerBand * |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 972 | EchoCancellationImpl::NumCancellersRequired( |
| 973 | num_output_channels(), num_reverse_channels())); |
| 974 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 975 | const size_t new_aecm_render_queue_element_max_size = |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 976 | std::max(static_cast<size_t>(1), |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 977 | kMaxAllowedValuesOfSamplesPerBand * |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 978 | EchoControlMobileImpl::NumCancellersRequired( |
| 979 | num_output_channels(), num_reverse_channels())); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 980 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 981 | const size_t new_agc_render_queue_element_max_size = |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 982 | std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand); |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 983 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 984 | const size_t new_red_render_queue_element_max_size = |
| 985 | std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame); |
| 986 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 987 | // Reallocate the queues if the queue item sizes are too small to fit the |
| 988 | // data to put in the queues. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 989 | if (aec_render_queue_element_max_size_ < |
| 990 | new_aec_render_queue_element_max_size) { |
| 991 | aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size; |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 992 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 993 | std::vector<float> template_queue_element( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 994 | aec_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 995 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 996 | aec_render_signal_queue_.reset( |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 997 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 998 | kMaxNumFramesToBuffer, template_queue_element, |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 999 | RenderQueueItemVerifier<float>( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1000 | aec_render_queue_element_max_size_))); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1001 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1002 | aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_); |
| 1003 | aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1004 | } else { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1005 | aec_render_signal_queue_->Clear(); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1008 | if (aecm_render_queue_element_max_size_ < |
| 1009 | new_aecm_render_queue_element_max_size) { |
| 1010 | aecm_render_queue_element_max_size_ = |
| 1011 | new_aecm_render_queue_element_max_size; |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1012 | |
| 1013 | std::vector<int16_t> template_queue_element( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1014 | aecm_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1015 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1016 | aecm_render_signal_queue_.reset( |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1017 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 1018 | kMaxNumFramesToBuffer, template_queue_element, |
| 1019 | RenderQueueItemVerifier<int16_t>( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1020 | aecm_render_queue_element_max_size_))); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1021 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1022 | aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_); |
| 1023 | aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1024 | } else { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1025 | aecm_render_signal_queue_->Clear(); |
| 1026 | } |
| 1027 | |
| 1028 | if (agc_render_queue_element_max_size_ < |
| 1029 | new_agc_render_queue_element_max_size) { |
| 1030 | agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size; |
| 1031 | |
| 1032 | std::vector<int16_t> template_queue_element( |
| 1033 | agc_render_queue_element_max_size_); |
| 1034 | |
| 1035 | agc_render_signal_queue_.reset( |
| 1036 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 1037 | kMaxNumFramesToBuffer, template_queue_element, |
| 1038 | RenderQueueItemVerifier<int16_t>( |
| 1039 | agc_render_queue_element_max_size_))); |
| 1040 | |
| 1041 | agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_); |
| 1042 | agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_); |
| 1043 | } else { |
| 1044 | agc_render_signal_queue_->Clear(); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1045 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (red_render_queue_element_max_size_ < |
| 1048 | new_red_render_queue_element_max_size) { |
| 1049 | red_render_queue_element_max_size_ = new_red_render_queue_element_max_size; |
| 1050 | |
| 1051 | std::vector<float> template_queue_element( |
| 1052 | red_render_queue_element_max_size_); |
| 1053 | |
| 1054 | red_render_signal_queue_.reset( |
| 1055 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 1056 | kMaxNumFramesToBuffer, template_queue_element, |
| 1057 | RenderQueueItemVerifier<float>( |
| 1058 | red_render_queue_element_max_size_))); |
| 1059 | |
| 1060 | red_render_queue_buffer_.resize(red_render_queue_element_max_size_); |
| 1061 | red_capture_queue_buffer_.resize(red_render_queue_element_max_size_); |
| 1062 | } else { |
| 1063 | red_render_signal_queue_->Clear(); |
| 1064 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | void AudioProcessingImpl::EmptyQueuedRenderAudio() { |
| 1068 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1069 | while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1070 | public_submodules_->echo_cancellation->ProcessRenderAudio( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1071 | aec_capture_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1074 | while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) { |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1075 | public_submodules_->echo_control_mobile->ProcessRenderAudio( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1076 | aecm_capture_queue_buffer_); |
| 1077 | } |
| 1078 | |
| 1079 | while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) { |
| 1080 | public_submodules_->gain_control->ProcessRenderAudio( |
| 1081 | agc_capture_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1082 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1083 | |
| 1084 | while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) { |
| 1085 | private_submodules_->residual_echo_detector->AnalyzeRenderAudio( |
| 1086 | red_capture_queue_buffer_); |
| 1087 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1090 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1091 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1092 | { |
| 1093 | // Acquire the capture lock in order to safely call the function |
| 1094 | // that retrieves the render side data. This function accesses apm |
| 1095 | // getters that need the capture lock held when being called. |
| 1096 | // The lock needs to be released as |
| 1097 | // public_submodules_->echo_control_mobile->is_enabled() aquires this lock |
| 1098 | // as well. |
| 1099 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1100 | EmptyQueuedRenderAudio(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1101 | } |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 1102 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1103 | if (!frame) { |
| 1104 | return kNullPointerError; |
| 1105 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1106 | // Must be a native rate. |
| 1107 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 1108 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 1109 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 1110 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1111 | return kBadSampleRateError; |
| 1112 | } |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 1113 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1114 | ProcessingConfig processing_config; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1115 | bool reinitialization_required = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1116 | { |
| 1117 | // Aquire lock for the access of api_format. |
| 1118 | // The lock is released immediately due to the conditional |
| 1119 | // reinitialization. |
| 1120 | rtc::CritScope cs_capture(&crit_capture_); |
| 1121 | // TODO(ajm): The input and output rates and channels are currently |
| 1122 | // constrained to be identical in the int16 interface. |
| 1123 | processing_config = formats_.api_format; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1124 | |
| 1125 | reinitialization_required = UpdateActiveSubmoduleStates(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1126 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1127 | processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 1128 | processing_config.input_stream().set_num_channels(frame->num_channels_); |
| 1129 | processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 1130 | processing_config.output_stream().set_num_channels(frame->num_channels_); |
| 1131 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1132 | { |
| 1133 | // Do conditional reinitialization. |
| 1134 | rtc::CritScope cs_render(&crit_render_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1135 | RETURN_ON_ERR( |
| 1136 | MaybeInitializeCapture(processing_config, reinitialization_required)); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1137 | } |
| 1138 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 1139 | if (frame->samples_per_channel_ != |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1140 | formats_.api_format.input_stream().num_frames()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1141 | return kBadDataLengthError; |
| 1142 | } |
| 1143 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1144 | if (aec_dump_) { |
| 1145 | RecordUnprocessedCaptureStream(*frame); |
| 1146 | } |
| 1147 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1148 | capture_.capture_audio->DeinterleaveFrom(frame); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1149 | RETURN_ON_ERR(ProcessCaptureStreamLocked()); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1150 | capture_.capture_audio->InterleaveTo( |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame] | 1151 | frame, submodule_states_.CaptureMultiBandProcessingActive() || |
| 1152 | submodule_states_.CaptureFullBandProcessingActive()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1153 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1154 | if (aec_dump_) { |
| 1155 | RecordProcessedCaptureStream(*frame); |
| 1156 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1157 | |
| 1158 | return kNoError; |
| 1159 | } |
| 1160 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1161 | int AudioProcessingImpl::ProcessCaptureStreamLocked() { |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 1162 | // Ensure that not both the AEC and AECM are active at the same time. |
| 1163 | // TODO(peah): Simplify once the public API Enable functions for these |
| 1164 | // are moved to APM. |
| 1165 | RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() && |
| 1166 | public_submodules_->echo_control_mobile->is_enabled())); |
| 1167 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1168 | MaybeUpdateHistograms(); |
| 1169 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1170 | AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1171 | |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1172 | capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>( |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 1173 | capture_buffer->channels_const()[0], |
| 1174 | capture_nonlocked_.capture_processing_format.num_frames())); |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1175 | const bool log_rms = ++capture_rms_interval_counter_ >= 1000; |
| 1176 | if (log_rms) { |
| 1177 | capture_rms_interval_counter_ = 0; |
| 1178 | RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak(); |
henrik.lundin | 45bb513 | 2016-12-06 04:28:04 -0800 | [diff] [blame] | 1179 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms", |
| 1180 | levels.average, 1, RmsLevel::kMinLevelDb, 64); |
| 1181 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms", |
| 1182 | levels.peak, 1, RmsLevel::kMinLevelDb, 64); |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1185 | if (private_submodules_->echo_controller) { |
Per Ã…hgren | 9aed31c | 2017-06-29 20:23:27 +0200 | [diff] [blame] | 1186 | // TODO(peah): Reactivate analogue AGC gain detection once the analogue AGC |
| 1187 | // issues have been addressed. |
| 1188 | capture_.echo_path_gain_change = false; |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1189 | private_submodules_->echo_controller->AnalyzeCapture(capture_buffer); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1192 | if (constants_.use_experimental_agc && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1193 | public_submodules_->gain_control->is_enabled()) { |
| 1194 | private_submodules_->agc_manager->AnalyzePreProcess( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1195 | capture_buffer->channels()[0], capture_buffer->num_channels(), |
| 1196 | capture_nonlocked_.capture_processing_format.num_frames()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1199 | if (submodule_states_.CaptureMultiBandSubModulesActive() && |
| 1200 | SampleRateSupportsMultiBand( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1201 | capture_nonlocked_.capture_processing_format.sample_rate_hz())) { |
| 1202 | capture_buffer->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1205 | if (private_submodules_->echo_controller) { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1206 | // Force down-mixing of the number of channels after the detection of |
| 1207 | // capture signal saturation. |
| 1208 | // TODO(peah): Look into ensuring that this kind of tampering with the |
| 1209 | // AudioBuffer functionality should not be needed. |
| 1210 | capture_buffer->set_num_channels(1); |
| 1211 | } |
| 1212 | |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1213 | if (capture_nonlocked_.beamformer_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1214 | private_submodules_->beamformer->AnalyzeChunk( |
| 1215 | *capture_buffer->split_data_f()); |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1216 | // Discards all channels by the leftmost one. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1217 | capture_buffer->set_num_channels(1); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1218 | } |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1219 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1220 | // TODO(peah): Move the AEC3 low-cut filter to this place. |
| 1221 | if (private_submodules_->low_cut_filter && |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1222 | !private_submodules_->echo_controller) { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1223 | private_submodules_->low_cut_filter->Process(capture_buffer); |
| 1224 | } |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1225 | RETURN_ON_ERR( |
| 1226 | public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer)); |
| 1227 | public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer); |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 1228 | |
| 1229 | // Ensure that the stream delay was set before the call to the |
| 1230 | // AEC ProcessCaptureAudio function. |
| 1231 | if (public_submodules_->echo_cancellation->is_enabled() && |
| 1232 | !was_stream_delay_set()) { |
| 1233 | return AudioProcessing::kStreamParameterNotSetError; |
| 1234 | } |
| 1235 | |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1236 | if (private_submodules_->echo_controller) { |
| 1237 | private_submodules_->echo_controller->ProcessCapture( |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 1238 | capture_buffer, capture_.echo_path_gain_change); |
peah | 61202ac | 2017-02-06 03:39:42 -0800 | [diff] [blame] | 1239 | } else { |
| 1240 | RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio( |
| 1241 | capture_buffer, stream_delay_ms())); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1244 | if (public_submodules_->echo_control_mobile->is_enabled() && |
| 1245 | public_submodules_->noise_suppression->is_enabled()) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1246 | capture_buffer->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1247 | } |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1248 | public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1249 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1250 | if (capture_nonlocked_.intelligibility_enabled) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1251 | RTC_DCHECK(public_submodules_->noise_suppression->is_enabled()); |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1252 | int gain_db = public_submodules_->gain_control->is_enabled() ? |
| 1253 | public_submodules_->gain_control->compression_gain_db() : |
| 1254 | 0; |
Alejandro Luebs | 5041110 | 2016-06-30 15:35:41 -0700 | [diff] [blame] | 1255 | float gain = std::pow(10.f, gain_db / 20.f); |
| 1256 | gain *= capture_nonlocked_.level_controller_enabled ? |
| 1257 | private_submodules_->level_controller->GetLastGain() : |
| 1258 | 1.f; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1259 | public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate( |
Alejandro Luebs | 5041110 | 2016-06-30 15:35:41 -0700 | [diff] [blame] | 1260 | public_submodules_->noise_suppression->NoiseEstimate(), gain); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1261 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1262 | #endif |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 1263 | |
| 1264 | // Ensure that the stream delay was set before the call to the |
| 1265 | // AECM ProcessCaptureAudio function. |
| 1266 | if (public_submodules_->echo_control_mobile->is_enabled() && |
| 1267 | !was_stream_delay_set()) { |
| 1268 | return AudioProcessing::kStreamParameterNotSetError; |
| 1269 | } |
| 1270 | |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1271 | if (!(private_submodules_->echo_controller || |
Per Ã…hgren | 46537a3 | 2017-06-07 10:08:10 +0200 | [diff] [blame] | 1272 | public_submodules_->echo_cancellation->is_enabled())) { |
| 1273 | RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio( |
| 1274 | capture_buffer, stream_delay_ms())); |
| 1275 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1276 | |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1277 | if (capture_nonlocked_.beamformer_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1278 | private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f()); |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1281 | public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1282 | |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1283 | if (constants_.use_experimental_agc && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1284 | public_submodules_->gain_control->is_enabled() && |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1285 | (!capture_nonlocked_.beamformer_enabled || |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1286 | private_submodules_->beamformer->is_target_present())) { |
| 1287 | private_submodules_->agc_manager->Process( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1288 | capture_buffer->split_bands_const(0)[kBand0To8kHz], |
| 1289 | capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1290 | } |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 1291 | RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1292 | capture_buffer, echo_cancellation()->stream_has_echo())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1293 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1294 | if (submodule_states_.CaptureMultiBandProcessingActive() && |
| 1295 | SampleRateSupportsMultiBand( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1296 | capture_nonlocked_.capture_processing_format.sample_rate_hz())) { |
| 1297 | capture_buffer->MergeFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 1300 | if (config_.residual_echo_detector.enabled) { |
| 1301 | private_submodules_->residual_echo_detector->AnalyzeCaptureAudio( |
| 1302 | rtc::ArrayView<const float>(capture_buffer->channels_f()[0], |
| 1303 | capture_buffer->num_frames())); |
| 1304 | } |
| 1305 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1306 | // TODO(aluebs): Investigate if the transient suppression placement should be |
| 1307 | // before or after the AGC. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1308 | if (capture_.transient_suppressor_enabled) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1309 | float voice_probability = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1310 | private_submodules_->agc_manager.get() |
| 1311 | ? private_submodules_->agc_manager->voice_probability() |
| 1312 | : 1.f; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1313 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1314 | public_submodules_->transient_suppressor->Suppress( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1315 | capture_buffer->channels_f()[0], capture_buffer->num_frames(), |
| 1316 | capture_buffer->num_channels(), |
| 1317 | capture_buffer->split_bands_const_f(0)[kBand0To8kHz], |
| 1318 | capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(), |
| 1319 | capture_buffer->num_keyboard_frames(), voice_probability, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1320 | capture_.key_pressed); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 1323 | if (config_.gain_controller2.enabled) { |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1324 | private_submodules_->gain_controller2->Process(capture_buffer); |
| 1325 | } |
| 1326 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1327 | if (capture_nonlocked_.level_controller_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1328 | private_submodules_->level_controller->Process(capture_buffer); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 1331 | if (private_submodules_->capture_post_processor) { |
| 1332 | private_submodules_->capture_post_processor->Process(capture_buffer); |
| 1333 | } |
| 1334 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 1335 | // The level estimator operates on the recombined data. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1336 | public_submodules_->level_estimator->ProcessStream(capture_buffer); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1337 | |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1338 | capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>( |
| 1339 | capture_buffer->channels_const()[0], |
| 1340 | capture_nonlocked_.capture_processing_format.num_frames())); |
| 1341 | if (log_rms) { |
| 1342 | RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak(); |
| 1343 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms", |
| 1344 | levels.average, 1, RmsLevel::kMinLevelDb, 64); |
| 1345 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms", |
| 1346 | levels.peak, 1, RmsLevel::kMinLevelDb, 64); |
| 1347 | } |
| 1348 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1349 | capture_.was_stream_delay_set = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1350 | return kNoError; |
| 1351 | } |
| 1352 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1353 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 1354 | size_t samples_per_channel, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1355 | int sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1356 | ChannelLayout layout) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1357 | TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1358 | rtc::CritScope cs(&crit_render_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1359 | const StreamConfig reverse_config = { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1360 | sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout), |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1361 | }; |
| 1362 | if (samples_per_channel != reverse_config.num_frames()) { |
| 1363 | return kBadDataLengthError; |
| 1364 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1365 | return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1368 | int AudioProcessingImpl::ProcessReverseStream(const float* const* src, |
| 1369 | const StreamConfig& input_config, |
| 1370 | const StreamConfig& output_config, |
| 1371 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1372 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1373 | rtc::CritScope cs(&crit_render_); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1374 | RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config)); |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 1375 | if (submodule_states_.RenderMultiBandProcessingActive() || |
| 1376 | submodule_states_.RenderFullBandProcessingActive()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1377 | render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(), |
| 1378 | dest); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1379 | } else if (formats_.api_format.reverse_input_stream() != |
| 1380 | formats_.api_format.reverse_output_stream()) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1381 | render_.render_converter->Convert(src, input_config.num_samples(), dest, |
| 1382 | output_config.num_samples()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1383 | } else { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1384 | CopyAudioIfNeeded(src, input_config.num_frames(), |
| 1385 | input_config.num_channels(), dest); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | return kNoError; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1391 | int AudioProcessingImpl::AnalyzeReverseStreamLocked( |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1392 | const float* const* src, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1393 | const StreamConfig& input_config, |
| 1394 | const StreamConfig& output_config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1395 | if (src == nullptr) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1396 | return kNullPointerError; |
| 1397 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1398 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1399 | if (input_config.num_channels() == 0) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1400 | return kBadNumberChannelsError; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1403 | ProcessingConfig processing_config = formats_.api_format; |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1404 | processing_config.reverse_input_stream() = input_config; |
| 1405 | processing_config.reverse_output_stream() = output_config; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1406 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1407 | RETURN_ON_ERR(MaybeInitializeRender(processing_config)); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1408 | assert(input_config.num_frames() == |
| 1409 | formats_.api_format.reverse_input_stream().num_frames()); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1410 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1411 | if (aec_dump_) { |
| 1412 | const size_t channel_size = |
| 1413 | formats_.api_format.reverse_input_stream().num_frames(); |
| 1414 | const size_t num_channels = |
| 1415 | formats_.api_format.reverse_input_stream().num_channels(); |
| 1416 | aec_dump_->WriteRenderStreamMessage( |
| 1417 | FloatAudioFrame(src, num_channels, channel_size)); |
| 1418 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1419 | render_.render_audio->CopyFrom(src, |
| 1420 | formats_.api_format.reverse_input_stream()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1421 | return ProcessRenderStreamLocked(); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1425 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1426 | rtc::CritScope cs(&crit_render_); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1427 | if (frame == nullptr) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1428 | return kNullPointerError; |
| 1429 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1430 | // Must be a native rate. |
| 1431 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 1432 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 1433 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 1434 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1435 | return kBadSampleRateError; |
| 1436 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 1437 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1438 | if (frame->num_channels_ <= 0) { |
| 1439 | return kBadNumberChannelsError; |
| 1440 | } |
| 1441 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1442 | ProcessingConfig processing_config = formats_.api_format; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1443 | processing_config.reverse_input_stream().set_sample_rate_hz( |
| 1444 | frame->sample_rate_hz_); |
| 1445 | processing_config.reverse_input_stream().set_num_channels( |
| 1446 | frame->num_channels_); |
| 1447 | processing_config.reverse_output_stream().set_sample_rate_hz( |
| 1448 | frame->sample_rate_hz_); |
| 1449 | processing_config.reverse_output_stream().set_num_channels( |
| 1450 | frame->num_channels_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1451 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1452 | RETURN_ON_ERR(MaybeInitializeRender(processing_config)); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1453 | if (frame->samples_per_channel_ != |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1454 | formats_.api_format.reverse_input_stream().num_frames()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1455 | return kBadDataLengthError; |
| 1456 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1457 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1458 | if (aec_dump_) { |
| 1459 | aec_dump_->WriteRenderStreamMessage(*frame); |
| 1460 | } |
| 1461 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1462 | render_.render_audio->DeinterleaveFrom(frame); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1463 | RETURN_ON_ERR(ProcessRenderStreamLocked()); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1464 | render_.render_audio->InterleaveTo( |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 1465 | frame, submodule_states_.RenderMultiBandProcessingActive() || |
| 1466 | submodule_states_.RenderFullBandProcessingActive()); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 1467 | return kNoError; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1468 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1469 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1470 | int AudioProcessingImpl::ProcessRenderStreamLocked() { |
| 1471 | AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity. |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 1472 | |
| 1473 | QueueNonbandedRenderAudio(render_buffer); |
| 1474 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 1475 | if (private_submodules_->render_pre_processor) { |
| 1476 | private_submodules_->render_pre_processor->Process(render_buffer); |
| 1477 | } |
| 1478 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1479 | if (submodule_states_.RenderMultiBandSubModulesActive() && |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1480 | SampleRateSupportsMultiBand( |
| 1481 | formats_.render_processing_format.sample_rate_hz())) { |
| 1482 | render_buffer->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1485 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1486 | if (capture_nonlocked_.intelligibility_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1487 | public_submodules_->intelligibility_enhancer->ProcessRenderAudio( |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 1488 | render_buffer); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1489 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1490 | #endif |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1491 | |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 1492 | if (submodule_states_.RenderMultiBandSubModulesActive()) { |
| 1493 | QueueBandedRenderAudio(render_buffer); |
| 1494 | } |
| 1495 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1496 | // TODO(peah): Perform the queueing Ãnside QueueRenderAudiuo(). |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1497 | if (private_submodules_->echo_controller) { |
| 1498 | private_submodules_->echo_controller->AnalyzeRender(render_buffer); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1499 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1500 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1501 | if (submodule_states_.RenderMultiBandProcessingActive() && |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1502 | SampleRateSupportsMultiBand( |
| 1503 | formats_.render_processing_format.sample_rate_hz())) { |
| 1504 | render_buffer->MergeFrequencyBands(); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1507 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1511 | rtc::CritScope cs(&crit_capture_); |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1512 | Error retval = kNoError; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1513 | capture_.was_stream_delay_set = true; |
| 1514 | delay += capture_.delay_offset_ms; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1515 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1516 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1517 | delay = 0; |
| 1518 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 1522 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1523 | delay = 500; |
| 1524 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1527 | capture_nonlocked_.stream_delay_ms = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1528 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | int AudioProcessingImpl::stream_delay_ms() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1532 | // Used as callback from submodules, hence locking is not allowed. |
| 1533 | return capture_nonlocked_.stream_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | bool AudioProcessingImpl::was_stream_delay_set() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1537 | // Used as callback from submodules, hence locking is not allowed. |
| 1538 | return capture_.was_stream_delay_set; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1541 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1542 | rtc::CritScope cs(&crit_capture_); |
| 1543 | capture_.key_pressed = key_pressed; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1546 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1547 | rtc::CritScope cs(&crit_capture_); |
| 1548 | capture_.delay_offset_ms = offset; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | int AudioProcessingImpl::delay_offset_ms() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1552 | rtc::CritScope cs(&crit_capture_); |
| 1553 | return capture_.delay_offset_ms; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1556 | void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) { |
| 1557 | RTC_DCHECK(aec_dump); |
| 1558 | rtc::CritScope cs_render(&crit_render_); |
| 1559 | rtc::CritScope cs_capture(&crit_capture_); |
| 1560 | |
| 1561 | // The previously attached AecDump will be destroyed with the |
| 1562 | // 'aec_dump' parameter, which is after locks are released. |
| 1563 | aec_dump_.swap(aec_dump); |
| 1564 | WriteAecDumpConfigMessage(true); |
| 1565 | aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format)); |
| 1566 | } |
| 1567 | |
| 1568 | void AudioProcessingImpl::DetachAecDump() { |
| 1569 | // The d-tor of a task-queue based AecDump blocks until all pending |
| 1570 | // tasks are done. This construction avoids blocking while holding |
| 1571 | // the render and capture locks. |
| 1572 | std::unique_ptr<AecDump> aec_dump = nullptr; |
| 1573 | { |
| 1574 | rtc::CritScope cs_render(&crit_render_); |
| 1575 | rtc::CritScope cs_capture(&crit_capture_); |
| 1576 | aec_dump = std::move(aec_dump_); |
| 1577 | } |
| 1578 | } |
| 1579 | |
ivoc | 4e477a1 | 2017-01-15 08:29:46 -0800 | [diff] [blame] | 1580 | AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() { |
| 1581 | residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1582 | echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1583 | echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1584 | a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1585 | } |
| 1586 | |
| 1587 | AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics( |
| 1588 | const AudioProcessingStatistics& other) = default; |
| 1589 | |
| 1590 | AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() = |
| 1591 | default; |
| 1592 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 1593 | // TODO(ivoc): Remove this when GetStatistics() becomes pure virtual. |
| 1594 | AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics() |
| 1595 | const { |
| 1596 | return AudioProcessingStatistics(); |
| 1597 | } |
| 1598 | |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 1599 | // TODO(ivoc): Remove this when GetStatistics() becomes pure virtual. |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 1600 | AudioProcessingStats AudioProcessing::GetStatistics( |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 1601 | bool has_remote_tracks) const { |
| 1602 | return AudioProcessingStats(); |
| 1603 | } |
| 1604 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 1605 | AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics() |
| 1606 | const { |
| 1607 | AudioProcessingStatistics stats; |
| 1608 | EchoCancellation::Metrics metrics; |
Gustaf Ullberg | 09b9fae | 2017-11-24 13:42:29 +0100 | [diff] [blame] | 1609 | if (private_submodules_->echo_controller) { |
| 1610 | rtc::CritScope cs_capture(&crit_capture_); |
| 1611 | auto ec_metrics = private_submodules_->echo_controller->GetMetrics(); |
| 1612 | float erl = static_cast<float>(ec_metrics.echo_return_loss); |
| 1613 | float erle = static_cast<float>(ec_metrics.echo_return_loss_enhancement); |
| 1614 | // Instant value will also be used for min, max and average. |
| 1615 | stats.echo_return_loss.Set(erl, erl, erl, erl); |
| 1616 | stats.echo_return_loss_enhancement.Set(erle, erle, erle, erle); |
| 1617 | } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) == |
| 1618 | Error::kNoError) { |
ivoc | d0a151c | 2016-11-02 09:14:37 -0700 | [diff] [blame] | 1619 | stats.a_nlp.Set(metrics.a_nlp); |
| 1620 | stats.divergent_filter_fraction = metrics.divergent_filter_fraction; |
| 1621 | stats.echo_return_loss.Set(metrics.echo_return_loss); |
| 1622 | stats.echo_return_loss_enhancement.Set( |
| 1623 | metrics.echo_return_loss_enhancement); |
| 1624 | stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss); |
| 1625 | } |
ivoc | 9c192b2 | 2017-03-16 04:22:14 -0700 | [diff] [blame] | 1626 | { |
| 1627 | rtc::CritScope cs_capture(&crit_capture_); |
| 1628 | stats.residual_echo_likelihood = |
| 1629 | private_submodules_->residual_echo_detector->echo_likelihood(); |
| 1630 | stats.residual_echo_likelihood_recent_max = |
| 1631 | private_submodules_->residual_echo_detector |
| 1632 | ->echo_likelihood_recent_max(); |
| 1633 | } |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 1634 | public_submodules_->echo_cancellation->GetDelayMetrics( |
| 1635 | &stats.delay_median, &stats.delay_standard_deviation, |
| 1636 | &stats.fraction_poor_delays); |
| 1637 | return stats; |
| 1638 | } |
| 1639 | |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 1640 | AudioProcessingStats AudioProcessingImpl::GetStatistics( |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 1641 | bool has_remote_tracks) const { |
| 1642 | AudioProcessingStats stats; |
| 1643 | if (has_remote_tracks) { |
| 1644 | EchoCancellation::Metrics metrics; |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 1645 | if (private_submodules_->echo_controller) { |
| 1646 | rtc::CritScope cs_capture(&crit_capture_); |
Gustaf Ullberg | 09b9fae | 2017-11-24 13:42:29 +0100 | [diff] [blame] | 1647 | auto ec_metrics = private_submodules_->echo_controller->GetMetrics(); |
| 1648 | stats.echo_return_loss = ec_metrics.echo_return_loss; |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 1649 | stats.echo_return_loss_enhancement = |
Gustaf Ullberg | 09b9fae | 2017-11-24 13:42:29 +0100 | [diff] [blame] | 1650 | ec_metrics.echo_return_loss_enhancement; |
Per Ã…hgren | 83c4a02 | 2017-11-27 12:07:09 +0100 | [diff] [blame] | 1651 | stats.delay_ms = ec_metrics.delay_ms; |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 1652 | } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) == |
| 1653 | Error::kNoError) { |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 1654 | if (metrics.divergent_filter_fraction != -1.0f) { |
| 1655 | stats.divergent_filter_fraction = |
| 1656 | rtc::Optional<double>(metrics.divergent_filter_fraction); |
| 1657 | } |
| 1658 | if (metrics.echo_return_loss.instant != -100) { |
| 1659 | stats.echo_return_loss = |
| 1660 | rtc::Optional<double>(metrics.echo_return_loss.instant); |
| 1661 | } |
| 1662 | if (metrics.echo_return_loss_enhancement.instant != -100) { |
| 1663 | stats.echo_return_loss_enhancement = |
| 1664 | rtc::Optional<double>(metrics.echo_return_loss_enhancement.instant); |
| 1665 | } |
| 1666 | } |
| 1667 | if (config_.residual_echo_detector.enabled) { |
| 1668 | rtc::CritScope cs_capture(&crit_capture_); |
| 1669 | stats.residual_echo_likelihood = rtc::Optional<double>( |
| 1670 | private_submodules_->residual_echo_detector->echo_likelihood()); |
| 1671 | stats.residual_echo_likelihood_recent_max = |
| 1672 | rtc::Optional<double>(private_submodules_->residual_echo_detector |
| 1673 | ->echo_likelihood_recent_max()); |
| 1674 | } |
| 1675 | int delay_median, delay_std; |
| 1676 | float fraction_poor_delays; |
| 1677 | if (public_submodules_->echo_cancellation->GetDelayMetrics( |
| 1678 | &delay_median, &delay_std, &fraction_poor_delays) == |
| 1679 | Error::kNoError) { |
| 1680 | if (delay_median >= 0) { |
| 1681 | stats.delay_median_ms = rtc::Optional<int32_t>(delay_median); |
| 1682 | } |
| 1683 | if (delay_std >= 0) { |
| 1684 | stats.delay_standard_deviation_ms = rtc::Optional<int32_t>(delay_std); |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | return stats; |
| 1689 | } |
| 1690 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1691 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 1692 | return public_submodules_->echo_cancellation.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 1696 | return public_submodules_->echo_control_mobile.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | GainControl* AudioProcessingImpl::gain_control() const { |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1700 | if (constants_.use_experimental_agc) { |
| 1701 | return public_submodules_->gain_control_for_experimental_agc.get(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1702 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 1703 | return public_submodules_->gain_control.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1707 | return high_pass_filter_impl_.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
solenberg | 949028f | 2015-12-15 11:39:38 -0800 | [diff] [blame] | 1711 | return public_submodules_->level_estimator.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 1715 | return public_submodules_->noise_suppression.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
solenberg | a29386c | 2015-12-16 03:31:12 -0800 | [diff] [blame] | 1719 | return public_submodules_->voice_detection.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1722 | void AudioProcessingImpl::MutateConfig( |
| 1723 | rtc::FunctionView<void(AudioProcessing::Config*)> mutator) { |
| 1724 | rtc::CritScope cs_render(&crit_render_); |
| 1725 | rtc::CritScope cs_capture(&crit_capture_); |
| 1726 | mutator(&config_); |
| 1727 | ApplyConfig(config_); |
| 1728 | } |
| 1729 | |
| 1730 | AudioProcessing::Config AudioProcessingImpl::GetConfig() const { |
| 1731 | rtc::CritScope cs_render(&crit_render_); |
| 1732 | rtc::CritScope cs_capture(&crit_capture_); |
| 1733 | return config_; |
| 1734 | } |
| 1735 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1736 | bool AudioProcessingImpl::UpdateActiveSubmoduleStates() { |
| 1737 | return submodule_states_.Update( |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1738 | config_.high_pass_filter.enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1739 | public_submodules_->echo_cancellation->is_enabled(), |
| 1740 | public_submodules_->echo_control_mobile->is_enabled(), |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1741 | config_.residual_echo_detector.enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1742 | public_submodules_->noise_suppression->is_enabled(), |
| 1743 | capture_nonlocked_.intelligibility_enabled, |
| 1744 | capture_nonlocked_.beamformer_enabled, |
| 1745 | public_submodules_->gain_control->is_enabled(), |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 1746 | config_.gain_controller2.enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1747 | capture_nonlocked_.level_controller_enabled, |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 1748 | capture_nonlocked_.echo_controller_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1749 | public_submodules_->voice_detection->is_enabled(), |
| 1750 | public_submodules_->level_estimator->is_enabled(), |
| 1751 | capture_.transient_suppressor_enabled); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1754 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 1755 | void AudioProcessingImpl::InitializeTransient() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1756 | if (capture_.transient_suppressor_enabled) { |
| 1757 | if (!public_submodules_->transient_suppressor.get()) { |
| 1758 | public_submodules_->transient_suppressor.reset(new TransientSuppressor()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1759 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1760 | public_submodules_->transient_suppressor->Initialize( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1761 | capture_nonlocked_.capture_processing_format.sample_rate_hz(), |
| 1762 | capture_nonlocked_.split_rate, num_proc_channels()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1763 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1766 | void AudioProcessingImpl::InitializeBeamformer() { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1767 | if (capture_nonlocked_.beamformer_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1768 | if (!private_submodules_->beamformer) { |
| 1769 | private_submodules_->beamformer.reset(new NonlinearBeamformer( |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1770 | capture_.array_geometry, 1u, capture_.target_direction)); |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 1771 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1772 | private_submodules_->beamformer->Initialize(kChunkSizeMs, |
| 1773 | capture_nonlocked_.split_rate); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1777 | void AudioProcessingImpl::InitializeIntelligibility() { |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1778 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1779 | if (capture_nonlocked_.intelligibility_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1780 | public_submodules_->intelligibility_enhancer.reset( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 1781 | new IntelligibilityEnhancer(capture_nonlocked_.split_rate, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 1782 | render_.render_audio->num_channels(), |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 1783 | render_.render_audio->num_bands(), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 1784 | NoiseSuppressionImpl::num_noise_bins())); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1785 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1786 | #endif |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1789 | void AudioProcessingImpl::InitializeLowCutFilter() { |
| 1790 | if (config_.high_pass_filter.enabled) { |
| 1791 | private_submodules_->low_cut_filter.reset( |
| 1792 | new LowCutFilter(num_proc_channels(), proc_sample_rate_hz())); |
| 1793 | } else { |
| 1794 | private_submodules_->low_cut_filter.reset(); |
| 1795 | } |
| 1796 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1797 | |
Gustaf Ullberg | 8eb9c7d | 2017-10-14 08:28:46 +0200 | [diff] [blame] | 1798 | void AudioProcessingImpl::InitializeEchoController() { |
Gustaf Ullberg | 002ef28 | 2017-10-12 15:13:17 +0200 | [diff] [blame] | 1799 | if (echo_control_factory_) { |
| 1800 | private_submodules_->echo_controller = |
| 1801 | echo_control_factory_->Create(proc_sample_rate_hz()); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1802 | } else { |
Gustaf Ullberg | 59ff0e2 | 2017-10-09 10:20:34 +0200 | [diff] [blame] | 1803 | private_submodules_->echo_controller.reset(); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1804 | } |
| 1805 | } |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1806 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1807 | void AudioProcessingImpl::InitializeGainController2() { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 1808 | if (config_.gain_controller2.enabled) { |
| 1809 | private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz()); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1813 | void AudioProcessingImpl::InitializeLevelController() { |
| 1814 | private_submodules_->level_controller->Initialize(proc_sample_rate_hz()); |
| 1815 | } |
| 1816 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1817 | void AudioProcessingImpl::InitializeResidualEchoDetector() { |
| 1818 | private_submodules_->residual_echo_detector->Initialize(); |
| 1819 | } |
| 1820 | |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 1821 | void AudioProcessingImpl::InitializePostProcessor() { |
| 1822 | if (private_submodules_->capture_post_processor) { |
| 1823 | private_submodules_->capture_post_processor->Initialize( |
| 1824 | proc_sample_rate_hz(), num_proc_channels()); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame^] | 1828 | void AudioProcessingImpl::InitializePreProcessor() { |
| 1829 | if (private_submodules_->render_pre_processor) { |
| 1830 | private_submodules_->render_pre_processor->Initialize( |
| 1831 | formats_.render_processing_format.sample_rate_hz(), |
| 1832 | formats_.render_processing_format.num_channels()); |
| 1833 | } |
| 1834 | } |
| 1835 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1836 | void AudioProcessingImpl::MaybeUpdateHistograms() { |
Bjorn Volcker | d92f267 | 2015-07-05 10:46:01 +0200 | [diff] [blame] | 1837 | static const int kMinDiffDelayMs = 60; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1838 | |
| 1839 | if (echo_cancellation()->is_enabled()) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 1840 | // Activate delay_jumps_ counters if we know echo_cancellation is running. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1841 | // If a stream has echo we know that the echo_cancellation is in process. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1842 | if (capture_.stream_delay_jumps == -1 && |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1843 | echo_cancellation()->stream_has_echo()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1844 | capture_.stream_delay_jumps = 0; |
| 1845 | } |
| 1846 | if (capture_.aec_system_delay_jumps == -1 && |
| 1847 | echo_cancellation()->stream_has_echo()) { |
| 1848 | capture_.aec_system_delay_jumps = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1851 | // Detect a jump in platform reported system delay and log the difference. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1852 | const int diff_stream_delay_ms = |
| 1853 | capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms; |
| 1854 | if (diff_stream_delay_ms > kMinDiffDelayMs && |
| 1855 | capture_.last_stream_delay_ms != 0) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1856 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump", |
| 1857 | diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1858 | if (capture_.stream_delay_jumps == -1) { |
| 1859 | capture_.stream_delay_jumps = 0; // Activate counter if needed. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1860 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1861 | capture_.stream_delay_jumps++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1862 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1863 | capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1864 | |
| 1865 | // Detect a jump in AEC system delay and log the difference. |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1866 | const int samples_per_ms = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1867 | rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000); |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1868 | RTC_DCHECK_LT(0, samples_per_ms); |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1869 | const int aec_system_delay_ms = |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1870 | public_submodules_->echo_cancellation->GetSystemDelayInSamples() / |
| 1871 | samples_per_ms; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1872 | const int diff_aec_system_delay_ms = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1873 | aec_system_delay_ms - capture_.last_aec_system_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1874 | if (diff_aec_system_delay_ms > kMinDiffDelayMs && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1875 | capture_.last_aec_system_delay_ms != 0) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1876 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump", |
| 1877 | diff_aec_system_delay_ms, kMinDiffDelayMs, 1000, |
| 1878 | 100); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1879 | if (capture_.aec_system_delay_jumps == -1) { |
| 1880 | capture_.aec_system_delay_jumps = 0; // Activate counter if needed. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1881 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1882 | capture_.aec_system_delay_jumps++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1883 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1884 | capture_.last_aec_system_delay_ms = aec_system_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1888 | void AudioProcessingImpl::UpdateHistogramsOnCallEnd() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1889 | // Run in a single-threaded manner. |
| 1890 | rtc::CritScope cs_render(&crit_render_); |
| 1891 | rtc::CritScope cs_capture(&crit_capture_); |
| 1892 | |
| 1893 | if (capture_.stream_delay_jumps > -1) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1894 | RTC_HISTOGRAM_ENUMERATION( |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1895 | "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps", |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1896 | capture_.stream_delay_jumps, 51); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1897 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1898 | capture_.stream_delay_jumps = -1; |
| 1899 | capture_.last_stream_delay_ms = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1900 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1901 | if (capture_.aec_system_delay_jumps > -1) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1902 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps", |
| 1903 | capture_.aec_system_delay_jumps, 51); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1904 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1905 | capture_.aec_system_delay_jumps = -1; |
| 1906 | capture_.last_aec_system_delay_ms = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1909 | void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) { |
| 1910 | if (!aec_dump_) { |
| 1911 | return; |
| 1912 | } |
| 1913 | std::string experiments_description = |
| 1914 | public_submodules_->echo_cancellation->GetExperimentsDescription(); |
| 1915 | // TODO(peah): Add semicolon-separated concatenations of experiment |
| 1916 | // descriptions for other submodules. |
| 1917 | if (capture_nonlocked_.level_controller_enabled) { |
| 1918 | experiments_description += "LevelController;"; |
| 1919 | } |
| 1920 | if (constants_.agc_clipped_level_min != kClippedLevelMin) { |
| 1921 | experiments_description += "AgcClippingLevelExperiment;"; |
| 1922 | } |
Gustaf Ullberg | ce045ac | 2017-10-16 13:49:04 +0200 | [diff] [blame] | 1923 | if (capture_nonlocked_.echo_controller_enabled) { |
| 1924 | experiments_description += "EchoController;"; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1925 | } |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 1926 | if (config_.gain_controller2.enabled) { |
| 1927 | experiments_description += "GainController2;"; |
| 1928 | } |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 1929 | |
| 1930 | InternalAPMConfig apm_config; |
| 1931 | |
| 1932 | apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled(); |
| 1933 | apm_config.aec_delay_agnostic_enabled = |
| 1934 | public_submodules_->echo_cancellation->is_delay_agnostic_enabled(); |
| 1935 | apm_config.aec_drift_compensation_enabled = |
| 1936 | public_submodules_->echo_cancellation->is_drift_compensation_enabled(); |
| 1937 | apm_config.aec_extended_filter_enabled = |
| 1938 | public_submodules_->echo_cancellation->is_extended_filter_enabled(); |
| 1939 | apm_config.aec_suppression_level = static_cast<int>( |
| 1940 | public_submodules_->echo_cancellation->suppression_level()); |
| 1941 | |
| 1942 | apm_config.aecm_enabled = |
| 1943 | public_submodules_->echo_control_mobile->is_enabled(); |
| 1944 | apm_config.aecm_comfort_noise_enabled = |
| 1945 | public_submodules_->echo_control_mobile->is_comfort_noise_enabled(); |
| 1946 | apm_config.aecm_routing_mode = |
| 1947 | static_cast<int>(public_submodules_->echo_control_mobile->routing_mode()); |
| 1948 | |
| 1949 | apm_config.agc_enabled = public_submodules_->gain_control->is_enabled(); |
| 1950 | apm_config.agc_mode = |
| 1951 | static_cast<int>(public_submodules_->gain_control->mode()); |
| 1952 | apm_config.agc_limiter_enabled = |
| 1953 | public_submodules_->gain_control->is_limiter_enabled(); |
| 1954 | apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc; |
| 1955 | |
| 1956 | apm_config.hpf_enabled = config_.high_pass_filter.enabled; |
| 1957 | |
| 1958 | apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled(); |
| 1959 | apm_config.ns_level = |
| 1960 | static_cast<int>(public_submodules_->noise_suppression->level()); |
| 1961 | |
| 1962 | apm_config.transient_suppression_enabled = |
| 1963 | capture_.transient_suppressor_enabled; |
| 1964 | apm_config.intelligibility_enhancer_enabled = |
| 1965 | capture_nonlocked_.intelligibility_enabled; |
| 1966 | apm_config.experiments_description = experiments_description; |
| 1967 | |
| 1968 | if (!forced && apm_config == apm_config_for_aec_dump_) { |
| 1969 | return; |
| 1970 | } |
| 1971 | aec_dump_->WriteConfig(apm_config); |
| 1972 | apm_config_for_aec_dump_ = apm_config; |
| 1973 | } |
| 1974 | |
| 1975 | void AudioProcessingImpl::RecordUnprocessedCaptureStream( |
| 1976 | const float* const* src) { |
| 1977 | RTC_DCHECK(aec_dump_); |
| 1978 | WriteAecDumpConfigMessage(false); |
| 1979 | |
| 1980 | const size_t channel_size = formats_.api_format.input_stream().num_frames(); |
| 1981 | const size_t num_channels = formats_.api_format.input_stream().num_channels(); |
| 1982 | aec_dump_->AddCaptureStreamInput( |
| 1983 | FloatAudioFrame(src, num_channels, channel_size)); |
| 1984 | RecordAudioProcessingState(); |
| 1985 | } |
| 1986 | |
| 1987 | void AudioProcessingImpl::RecordUnprocessedCaptureStream( |
| 1988 | const AudioFrame& capture_frame) { |
| 1989 | RTC_DCHECK(aec_dump_); |
| 1990 | WriteAecDumpConfigMessage(false); |
| 1991 | |
| 1992 | aec_dump_->AddCaptureStreamInput(capture_frame); |
| 1993 | RecordAudioProcessingState(); |
| 1994 | } |
| 1995 | |
| 1996 | void AudioProcessingImpl::RecordProcessedCaptureStream( |
| 1997 | const float* const* processed_capture_stream) { |
| 1998 | RTC_DCHECK(aec_dump_); |
| 1999 | |
| 2000 | const size_t channel_size = formats_.api_format.output_stream().num_frames(); |
| 2001 | const size_t num_channels = |
| 2002 | formats_.api_format.output_stream().num_channels(); |
| 2003 | aec_dump_->AddCaptureStreamOutput( |
| 2004 | FloatAudioFrame(processed_capture_stream, num_channels, channel_size)); |
| 2005 | aec_dump_->WriteCaptureStreamMessage(); |
| 2006 | } |
| 2007 | |
| 2008 | void AudioProcessingImpl::RecordProcessedCaptureStream( |
| 2009 | const AudioFrame& processed_capture_frame) { |
| 2010 | RTC_DCHECK(aec_dump_); |
| 2011 | |
| 2012 | aec_dump_->AddCaptureStreamOutput(processed_capture_frame); |
| 2013 | aec_dump_->WriteCaptureStreamMessage(); |
| 2014 | } |
| 2015 | |
| 2016 | void AudioProcessingImpl::RecordAudioProcessingState() { |
| 2017 | RTC_DCHECK(aec_dump_); |
| 2018 | AecDump::AudioProcessingState audio_proc_state; |
| 2019 | audio_proc_state.delay = capture_nonlocked_.stream_delay_ms; |
| 2020 | audio_proc_state.drift = |
| 2021 | public_submodules_->echo_cancellation->stream_drift_samples(); |
| 2022 | audio_proc_state.level = gain_control()->stream_analog_level(); |
| 2023 | audio_proc_state.keypress = capture_.key_pressed; |
| 2024 | aec_dump_->AddAudioProcessingState(audio_proc_state); |
| 2025 | } |
| 2026 | |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 2027 | AudioProcessingImpl::ApmCaptureState::ApmCaptureState( |
| 2028 | bool transient_suppressor_enabled, |
| 2029 | const std::vector<Point>& array_geometry, |
| 2030 | SphericalPointf target_direction) |
| 2031 | : aec_system_delay_jumps(-1), |
| 2032 | delay_offset_ms(0), |
| 2033 | was_stream_delay_set(false), |
| 2034 | last_stream_delay_ms(0), |
| 2035 | last_aec_system_delay_ms(0), |
| 2036 | stream_delay_jumps(-1), |
| 2037 | output_will_be_muted(false), |
| 2038 | key_pressed(false), |
| 2039 | transient_suppressor_enabled(transient_suppressor_enabled), |
| 2040 | array_geometry(array_geometry), |
| 2041 | target_direction(target_direction), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 2042 | capture_processing_format(kSampleRate16kHz), |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 2043 | split_rate(kSampleRate16kHz), |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 2044 | echo_path_gain_change(false) {} |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 2045 | |
| 2046 | AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default; |
| 2047 | |
| 2048 | AudioProcessingImpl::ApmRenderState::ApmRenderState() = default; |
| 2049 | |
| 2050 | AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default; |
| 2051 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2052 | } // namespace webrtc |