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