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