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