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