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