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