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