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