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