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" |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/include/audio_util.h" |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 19 | #include "webrtc/common_audio/channel_buffer.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 20 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 21 | extern "C" { |
| 22 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 23 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 25 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
mgraczyk@chromium.org | 0f663de | 2015-03-13 00:13:32 +0000 | [diff] [blame] | 26 | #include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 27 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 28 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 29 | #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
| 30 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
| 31 | #include "webrtc/modules/audio_processing/high_pass_filter_impl.h" |
| 32 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
| 33 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
| 34 | #include "webrtc/modules/audio_processing/processing_component.h" |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 35 | #include "webrtc/modules/audio_processing/transient/transient_suppressor.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 36 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
| 37 | #include "webrtc/modules/interface/module_common_types.h" |
| 38 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 39 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 40 | #include "webrtc/system_wrappers/interface/logging.h" |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 41 | #include "webrtc/system_wrappers/interface/metrics.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 44 | // Files generated at build-time by the protobuf compiler. |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 45 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 46 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 47 | #else |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 48 | #include "webrtc/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 49 | #endif |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 50 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 52 | #define RETURN_ON_ERR(expr) \ |
| 53 | do { \ |
| 54 | int err = (expr); \ |
| 55 | if (err != kNoError) { \ |
| 56 | return err; \ |
| 57 | } \ |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 58 | } while (0) |
| 59 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | namespace webrtc { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) { |
| 64 | switch (layout) { |
| 65 | case AudioProcessing::kMono: |
| 66 | case AudioProcessing::kStereo: |
| 67 | return false; |
| 68 | case AudioProcessing::kMonoAndKeyboard: |
| 69 | case AudioProcessing::kStereoAndKeyboard: |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | assert(false); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | } // namespace |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 78 | |
| 79 | // Throughout webrtc, it's assumed that success is represented by zero. |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 80 | static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero"); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 81 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 82 | // This class has two main functionalities: |
| 83 | // |
| 84 | // 1) It is returned instead of the real GainControl after the new AGC has been |
| 85 | // enabled in order to prevent an outside user from overriding compression |
| 86 | // settings. It doesn't do anything in its implementation, except for |
| 87 | // delegating the const methods and Enable calls to the real GainControl, so |
| 88 | // AGC can still be disabled. |
| 89 | // |
| 90 | // 2) It is injected into AgcManagerDirect and implements volume callbacks for |
| 91 | // getting and setting the volume level. It just caches this value to be used |
| 92 | // in VoiceEngine later. |
| 93 | class GainControlForNewAgc : public GainControl, public VolumeCallbacks { |
| 94 | public: |
| 95 | explicit GainControlForNewAgc(GainControlImpl* gain_control) |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 96 | : real_gain_control_(gain_control), volume_(0) {} |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 97 | |
| 98 | // GainControl implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 99 | int Enable(bool enable) override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 100 | return real_gain_control_->Enable(enable); |
| 101 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 102 | bool is_enabled() const override { return real_gain_control_->is_enabled(); } |
| 103 | int set_stream_analog_level(int level) override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 104 | volume_ = level; |
| 105 | return AudioProcessing::kNoError; |
| 106 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 107 | int stream_analog_level() override { return volume_; } |
| 108 | int set_mode(Mode mode) override { return AudioProcessing::kNoError; } |
| 109 | Mode mode() const override { return GainControl::kAdaptiveAnalog; } |
| 110 | int set_target_level_dbfs(int level) override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 111 | return AudioProcessing::kNoError; |
| 112 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 113 | int target_level_dbfs() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 114 | return real_gain_control_->target_level_dbfs(); |
| 115 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 116 | int set_compression_gain_db(int gain) override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 117 | return AudioProcessing::kNoError; |
| 118 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 119 | int compression_gain_db() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 120 | return real_gain_control_->compression_gain_db(); |
| 121 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 122 | int enable_limiter(bool enable) override { return AudioProcessing::kNoError; } |
| 123 | bool is_limiter_enabled() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 124 | return real_gain_control_->is_limiter_enabled(); |
| 125 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 126 | int set_analog_level_limits(int minimum, int maximum) override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 127 | return AudioProcessing::kNoError; |
| 128 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 129 | int analog_level_minimum() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 130 | return real_gain_control_->analog_level_minimum(); |
| 131 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 132 | int analog_level_maximum() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 133 | return real_gain_control_->analog_level_maximum(); |
| 134 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 135 | bool stream_is_saturated() const override { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 136 | return real_gain_control_->stream_is_saturated(); |
| 137 | } |
| 138 | |
| 139 | // VolumeCallbacks implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 140 | void SetMicVolume(int volume) override { volume_ = volume; } |
| 141 | int GetMicVolume() override { return volume_; } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | GainControl* real_gain_control_; |
| 145 | int volume_; |
| 146 | }; |
| 147 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 148 | AudioProcessing* AudioProcessing::Create() { |
| 149 | Config config; |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 150 | return Create(config, nullptr); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | AudioProcessing* AudioProcessing::Create(const Config& config) { |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 154 | return Create(config, nullptr); |
| 155 | } |
| 156 | |
| 157 | AudioProcessing* AudioProcessing::Create(const Config& config, |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 158 | Beamformer<float>* beamformer) { |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 159 | AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | if (apm->Initialize() != kNoError) { |
| 161 | delete apm; |
| 162 | apm = NULL; |
| 163 | } |
| 164 | |
| 165 | return apm; |
| 166 | } |
| 167 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 168 | AudioProcessingImpl::AudioProcessingImpl(const Config& config) |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 169 | : AudioProcessingImpl(config, nullptr) {} |
| 170 | |
| 171 | AudioProcessingImpl::AudioProcessingImpl(const Config& config, |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 172 | Beamformer<float>* beamformer) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 173 | : echo_cancellation_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | echo_control_mobile_(NULL), |
| 175 | gain_control_(NULL), |
| 176 | high_pass_filter_(NULL), |
| 177 | level_estimator_(NULL), |
| 178 | noise_suppression_(NULL), |
| 179 | voice_detection_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | crit_(CriticalSectionWrapper::CreateCriticalSection()), |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 181 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 182 | debug_file_(FileWrapper::Create()), |
| 183 | event_msg_(new audioproc::Event()), |
| 184 | #endif |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 185 | api_format_({{{kSampleRate16kHz, 1, false}, |
| 186 | {kSampleRate16kHz, 1, false}, |
| 187 | {kSampleRate16kHz, 1, false}}}), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 188 | fwd_proc_format_(kSampleRate16kHz), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 189 | rev_proc_format_(kSampleRate16kHz, 1), |
| 190 | split_rate_(kSampleRate16kHz), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | stream_delay_ms_(0), |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 192 | delay_offset_ms_(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | was_stream_delay_set_(false), |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 194 | last_stream_delay_ms_(0), |
| 195 | last_aec_system_delay_ms_(0), |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 196 | stream_delay_jumps_(-1), |
| 197 | aec_system_delay_jumps_(-1), |
andrew@webrtc.org | 38bf249 | 2014-02-13 17:43:44 +0000 | [diff] [blame] | 198 | output_will_be_muted_(false), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 199 | key_pressed_(false), |
| 200 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
| 201 | use_new_agc_(false), |
| 202 | #else |
| 203 | use_new_agc_(config.Get<ExperimentalAgc>().enabled), |
| 204 | #endif |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 205 | agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume), |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 206 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
| 207 | transient_suppressor_enabled_(false), |
| 208 | #else |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 209 | transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled), |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 210 | #endif |
aluebs@webrtc.org | fb7a039 | 2015-01-05 21:58:58 +0000 | [diff] [blame] | 211 | beamformer_enabled_(config.Get<Beamforming>().enabled), |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 212 | beamformer_(beamformer), |
aluebs@webrtc.org | c9ce07e | 2015-03-02 20:07:31 +0000 | [diff] [blame] | 213 | array_geometry_(config.Get<Beamforming>().array_geometry), |
| 214 | supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 215 | echo_cancellation_ = new EchoCancellationImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | component_list_.push_back(echo_cancellation_); |
| 217 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 218 | echo_control_mobile_ = new EchoControlMobileImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | component_list_.push_back(echo_control_mobile_); |
| 220 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 221 | gain_control_ = new GainControlImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | component_list_.push_back(gain_control_); |
| 223 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 224 | high_pass_filter_ = new HighPassFilterImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | component_list_.push_back(high_pass_filter_); |
| 226 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 227 | level_estimator_ = new LevelEstimatorImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | component_list_.push_back(level_estimator_); |
| 229 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 230 | noise_suppression_ = new NoiseSuppressionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | component_list_.push_back(noise_suppression_); |
| 232 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 233 | voice_detection_ = new VoiceDetectionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | component_list_.push_back(voice_detection_); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 235 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 236 | gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_)); |
| 237 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 238 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | AudioProcessingImpl::~AudioProcessingImpl() { |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 242 | { |
| 243 | CriticalSectionScoped crit_scoped(crit_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 244 | // Depends on gain_control_ and gain_control_for_new_agc_. |
| 245 | agc_manager_.reset(); |
| 246 | // Depends on gain_control_. |
| 247 | gain_control_for_new_agc_.reset(); |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 248 | while (!component_list_.empty()) { |
| 249 | ProcessingComponent* component = component_list_.front(); |
| 250 | component->Destroy(); |
| 251 | delete component; |
| 252 | component_list_.pop_front(); |
| 253 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 255 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 256 | if (debug_file_->Open()) { |
| 257 | debug_file_->CloseFile(); |
| 258 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 259 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | } |
andrew@webrtc.org | 16cfbe2 | 2012-08-29 16:58:25 +0000 | [diff] [blame] | 261 | delete crit_; |
| 262 | crit_ = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | } |
| 264 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | int AudioProcessingImpl::Initialize() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 266 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 267 | return InitializeLocked(); |
| 268 | } |
| 269 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 270 | int AudioProcessingImpl::set_sample_rate_hz(int rate) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 271 | CriticalSectionScoped crit_scoped(crit_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 272 | |
| 273 | ProcessingConfig processing_config = api_format_; |
| 274 | processing_config.input_stream().set_sample_rate_hz(rate); |
| 275 | processing_config.output_stream().set_sample_rate_hz(rate); |
| 276 | return InitializeLocked(processing_config); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | int AudioProcessingImpl::Initialize(int input_sample_rate_hz, |
| 280 | int output_sample_rate_hz, |
| 281 | int reverse_sample_rate_hz, |
| 282 | ChannelLayout input_layout, |
| 283 | ChannelLayout output_layout, |
| 284 | ChannelLayout reverse_layout) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 285 | const ProcessingConfig processing_config = { |
| 286 | {{input_sample_rate_hz, ChannelsFromLayout(input_layout), |
| 287 | LayoutHasKeyboard(input_layout)}, |
| 288 | {output_sample_rate_hz, ChannelsFromLayout(output_layout), |
| 289 | LayoutHasKeyboard(output_layout)}, |
| 290 | {reverse_sample_rate_hz, ChannelsFromLayout(reverse_layout), |
| 291 | LayoutHasKeyboard(reverse_layout)}}}; |
| 292 | |
| 293 | return Initialize(processing_config); |
| 294 | } |
| 295 | |
| 296 | int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 297 | CriticalSectionScoped crit_scoped(crit_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 298 | return InitializeLocked(processing_config); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 299 | } |
| 300 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | int AudioProcessingImpl::InitializeLocked() { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 302 | const int fwd_audio_buffer_channels = |
| 303 | beamformer_enabled_ ? api_format_.input_stream().num_channels() |
| 304 | : api_format_.output_stream().num_channels(); |
| 305 | if (api_format_.reverse_stream().num_channels() > 0) { |
| 306 | render_audio_.reset(new AudioBuffer( |
| 307 | api_format_.reverse_stream().num_frames(), |
| 308 | api_format_.reverse_stream().num_channels(), |
| 309 | rev_proc_format_.num_frames(), rev_proc_format_.num_channels(), |
| 310 | rev_proc_format_.num_frames())); |
| 311 | } else { |
| 312 | render_audio_.reset(nullptr); |
| 313 | } |
| 314 | capture_audio_.reset(new AudioBuffer( |
| 315 | api_format_.input_stream().num_frames(), |
| 316 | api_format_.input_stream().num_channels(), fwd_proc_format_.num_frames(), |
| 317 | fwd_audio_buffer_channels, api_format_.output_stream().num_frames())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 319 | // Initialize all components. |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 320 | for (auto item : component_list_) { |
| 321 | int err = item->Initialize(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | if (err != kNoError) { |
| 323 | return err; |
| 324 | } |
| 325 | } |
| 326 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 327 | InitializeExperimentalAgc(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 328 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 329 | InitializeTransient(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 330 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 331 | InitializeBeamformer(); |
| 332 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 333 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 334 | if (debug_file_->Open()) { |
| 335 | int err = WriteInitMessage(); |
| 336 | if (err != kNoError) { |
| 337 | return err; |
| 338 | } |
| 339 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 340 | #endif |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 341 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 342 | return kNoError; |
| 343 | } |
| 344 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 345 | int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) { |
| 346 | for (const auto& stream : config.streams) { |
| 347 | if (stream.num_channels() < 0) { |
| 348 | return kBadNumberChannelsError; |
| 349 | } |
| 350 | if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) { |
| 351 | return kBadSampleRateError; |
| 352 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 353 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 354 | |
| 355 | const int num_in_channels = config.input_stream().num_channels(); |
| 356 | const int num_out_channels = config.output_stream().num_channels(); |
| 357 | |
| 358 | // Need at least one input channel. |
| 359 | // Need either one output channel or as many outputs as there are inputs. |
| 360 | if (num_in_channels == 0 || |
| 361 | !(num_out_channels == 1 || num_out_channels == num_in_channels)) { |
Michael Graczyk | c204754 | 2015-07-22 21:06:11 -0700 | [diff] [blame] | 362 | return kBadNumberChannelsError; |
| 363 | } |
| 364 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 365 | if (beamformer_enabled_ && |
| 366 | (static_cast<size_t>(num_in_channels) != array_geometry_.size() || |
| 367 | num_out_channels > 1)) { |
| 368 | return kBadNumberChannelsError; |
| 369 | } |
| 370 | |
| 371 | api_format_ = config; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 372 | |
| 373 | // We process at the closest native rate >= min(input rate, output rate)... |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 374 | const int min_proc_rate = |
| 375 | std::min(api_format_.input_stream().sample_rate_hz(), |
| 376 | api_format_.output_stream().sample_rate_hz()); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 377 | int fwd_proc_rate; |
aluebs@webrtc.org | c9ce07e | 2015-03-02 20:07:31 +0000 | [diff] [blame] | 378 | if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) { |
| 379 | fwd_proc_rate = kSampleRate48kHz; |
| 380 | } else if (min_proc_rate > kSampleRate16kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 381 | fwd_proc_rate = kSampleRate32kHz; |
| 382 | } else if (min_proc_rate > kSampleRate8kHz) { |
| 383 | fwd_proc_rate = kSampleRate16kHz; |
| 384 | } else { |
| 385 | fwd_proc_rate = kSampleRate8kHz; |
| 386 | } |
| 387 | // ...with one exception. |
| 388 | if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) { |
| 389 | fwd_proc_rate = kSampleRate16kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 392 | fwd_proc_format_ = StreamConfig(fwd_proc_rate); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 393 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 394 | // We normally process the reverse stream at 16 kHz. Unless... |
| 395 | int rev_proc_rate = kSampleRate16kHz; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 396 | if (fwd_proc_format_.sample_rate_hz() == kSampleRate8kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 397 | // ...the forward stream is at 8 kHz. |
| 398 | rev_proc_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 399 | } else { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 400 | if (api_format_.reverse_stream().sample_rate_hz() == kSampleRate32kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 401 | // ...or the input is at 32 kHz, in which case we use the splitting |
| 402 | // filter rather than the resampler. |
| 403 | rev_proc_rate = kSampleRate32kHz; |
| 404 | } |
| 405 | } |
| 406 | |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 407 | // Always downmix the reverse stream to mono for analysis. This has been |
| 408 | // demonstrated to work well for AEC in most practical scenarios. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 409 | rev_proc_format_ = StreamConfig(rev_proc_rate, 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 410 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 411 | if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz || |
| 412 | fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 413 | split_rate_ = kSampleRate16kHz; |
| 414 | } else { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 415 | split_rate_ = fwd_proc_format_.sample_rate_hz(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return InitializeLocked(); |
| 419 | } |
| 420 | |
| 421 | // Calls InitializeLocked() if any of the audio parameters have changed from |
| 422 | // their current values. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 423 | int AudioProcessingImpl::MaybeInitializeLocked( |
| 424 | const ProcessingConfig& processing_config) { |
| 425 | if (processing_config == api_format_) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 426 | return kNoError; |
| 427 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 428 | return InitializeLocked(processing_config); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 429 | } |
| 430 | |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 431 | void AudioProcessingImpl::SetExtraOptions(const Config& config) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 432 | CriticalSectionScoped crit_scoped(crit_); |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 433 | for (auto item : component_list_) { |
| 434 | item->SetExtraOptions(config); |
| 435 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 436 | |
| 437 | if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) { |
| 438 | transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled; |
| 439 | InitializeTransient(); |
| 440 | } |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 441 | } |
| 442 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 443 | int AudioProcessingImpl::input_sample_rate_hz() const { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 444 | CriticalSectionScoped crit_scoped(crit_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 445 | return api_format_.input_stream().sample_rate_hz(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
andrew@webrtc.org | 46b31b1 | 2014-04-23 03:33:54 +0000 | [diff] [blame] | 448 | int AudioProcessingImpl::sample_rate_hz() const { |
| 449 | CriticalSectionScoped crit_scoped(crit_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 450 | return api_format_.input_stream().sample_rate_hz(); |
andrew@webrtc.org | 46b31b1 | 2014-04-23 03:33:54 +0000 | [diff] [blame] | 451 | } |
| 452 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 453 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 454 | return fwd_proc_format_.sample_rate_hz(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 455 | } |
| 456 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 457 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
| 458 | return split_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | int AudioProcessingImpl::num_reverse_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 462 | return rev_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | int AudioProcessingImpl::num_input_channels() const { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 466 | return api_format_.input_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | int AudioProcessingImpl::num_output_channels() const { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 470 | return api_format_.output_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 471 | } |
| 472 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 473 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 474 | CriticalSectionScoped lock(crit_); |
Bjorn Volcker | 424694c | 2015-03-27 11:30:43 +0100 | [diff] [blame] | 475 | output_will_be_muted_ = muted; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 476 | if (agc_manager_.get()) { |
| 477 | agc_manager_->SetCaptureMuted(output_will_be_muted_); |
| 478 | } |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | bool AudioProcessingImpl::output_will_be_muted() const { |
Bjorn Volcker | 424694c | 2015-03-27 11:30:43 +0100 | [diff] [blame] | 482 | CriticalSectionScoped lock(crit_); |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 483 | return output_will_be_muted_; |
| 484 | } |
| 485 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 486 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 487 | int samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 488 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 489 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 490 | int output_sample_rate_hz, |
| 491 | ChannelLayout output_layout, |
| 492 | float* const* dest) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 493 | StreamConfig input_stream = api_format_.input_stream(); |
| 494 | input_stream.set_sample_rate_hz(input_sample_rate_hz); |
| 495 | input_stream.set_num_channels(ChannelsFromLayout(input_layout)); |
| 496 | input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout)); |
| 497 | |
| 498 | StreamConfig output_stream = api_format_.output_stream(); |
| 499 | output_stream.set_sample_rate_hz(output_sample_rate_hz); |
| 500 | output_stream.set_num_channels(ChannelsFromLayout(output_layout)); |
| 501 | output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout)); |
| 502 | |
| 503 | if (samples_per_channel != input_stream.num_frames()) { |
| 504 | return kBadDataLengthError; |
| 505 | } |
| 506 | return ProcessStream(src, input_stream, output_stream, dest); |
| 507 | } |
| 508 | |
| 509 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
| 510 | const StreamConfig& input_config, |
| 511 | const StreamConfig& output_config, |
| 512 | float* const* dest) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 513 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 514 | if (!src || !dest) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 515 | return kNullPointerError; |
| 516 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 517 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 518 | ProcessingConfig processing_config = api_format_; |
| 519 | processing_config.input_stream() = input_config; |
| 520 | processing_config.output_stream() = output_config; |
| 521 | |
| 522 | RETURN_ON_ERR(MaybeInitializeLocked(processing_config)); |
| 523 | assert(processing_config.input_stream().num_frames() == |
| 524 | api_format_.input_stream().num_frames()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 525 | |
| 526 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 527 | if (debug_file_->Open()) { |
| 528 | event_msg_->set_type(audioproc::Event::STREAM); |
| 529 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 530 | const size_t channel_size = |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 531 | sizeof(float) * api_format_.input_stream().num_frames(); |
| 532 | for (int i = 0; i < api_format_.input_stream().num_channels(); ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 533 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 534 | } |
| 535 | #endif |
| 536 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 537 | capture_audio_->CopyFrom(src, api_format_.input_stream()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 538 | RETURN_ON_ERR(ProcessStreamLocked()); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 539 | capture_audio_->CopyTo(api_format_.output_stream(), dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 540 | |
| 541 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 542 | if (debug_file_->Open()) { |
| 543 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 544 | const size_t channel_size = |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 545 | sizeof(float) * api_format_.output_stream().num_frames(); |
| 546 | for (int i = 0; i < api_format_.output_stream().num_channels(); ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 547 | msg->add_output_channel(dest[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 548 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 549 | } |
| 550 | #endif |
| 551 | |
| 552 | return kNoError; |
| 553 | } |
| 554 | |
| 555 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
| 556 | CriticalSectionScoped crit_scoped(crit_); |
| 557 | if (!frame) { |
| 558 | return kNullPointerError; |
| 559 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 560 | // Must be a native rate. |
| 561 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 562 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 563 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 564 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 565 | return kBadSampleRateError; |
| 566 | } |
| 567 | if (echo_control_mobile_->is_enabled() && |
| 568 | frame->sample_rate_hz_ > kSampleRate16kHz) { |
| 569 | LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates"; |
| 570 | return kUnsupportedComponentError; |
| 571 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 572 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 573 | // TODO(ajm): The input and output rates and channels are currently |
| 574 | // constrained to be identical in the int16 interface. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 575 | ProcessingConfig processing_config = api_format_; |
| 576 | processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 577 | processing_config.input_stream().set_num_channels(frame->num_channels_); |
| 578 | processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 579 | processing_config.output_stream().set_num_channels(frame->num_channels_); |
| 580 | |
| 581 | RETURN_ON_ERR(MaybeInitializeLocked(processing_config)); |
| 582 | if (frame->samples_per_channel_ != api_format_.input_stream().num_frames()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | return kBadDataLengthError; |
| 584 | } |
| 585 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 586 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 587 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 588 | event_msg_->set_type(audioproc::Event::STREAM); |
| 589 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 590 | const size_t data_size = |
| 591 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 592 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 593 | } |
| 594 | #endif |
| 595 | |
| 596 | capture_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 597 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 598 | capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed())); |
| 599 | |
| 600 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 601 | if (debug_file_->Open()) { |
| 602 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 603 | const size_t data_size = |
| 604 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 605 | msg->set_output_data(frame->data_, data_size); |
| 606 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 607 | } |
| 608 | #endif |
| 609 | |
| 610 | return kNoError; |
| 611 | } |
| 612 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 613 | int AudioProcessingImpl::ProcessStreamLocked() { |
| 614 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 615 | if (debug_file_->Open()) { |
| 616 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 617 | msg->set_delay(stream_delay_ms_); |
| 618 | msg->set_drift(echo_cancellation_->stream_drift_samples()); |
bjornv@webrtc.org | 63da1dd | 2015-02-06 19:44:21 +0000 | [diff] [blame] | 619 | msg->set_level(gain_control()->stream_analog_level()); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 620 | msg->set_keypress(key_pressed_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 622 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 624 | MaybeUpdateHistograms(); |
| 625 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 626 | AudioBuffer* ca = capture_audio_.get(); // For brevity. |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 627 | if (use_new_agc_ && gain_control_->is_enabled()) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 628 | agc_manager_->AnalyzePreProcess(ca->channels()[0], ca->num_channels(), |
| 629 | fwd_proc_format_.num_frames()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 630 | } |
| 631 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 632 | bool data_processed = is_data_processed(); |
| 633 | if (analysis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 634 | ca->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | } |
| 636 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 637 | if (beamformer_enabled_) { |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 638 | beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f()); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 639 | ca->set_num_channels(1); |
| 640 | } |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 641 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 642 | RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca)); |
| 643 | RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca)); |
aluebs@webrtc.org | a0ce9fa | 2014-09-24 14:18:03 +0000 | [diff] [blame] | 644 | RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca)); |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 645 | RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 646 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 647 | if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 648 | ca->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 649 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 650 | RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca)); |
| 651 | RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca)); |
| 652 | RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 653 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 654 | if (use_new_agc_ && gain_control_->is_enabled() && |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 655 | (!beamformer_enabled_ || beamformer_->is_target_present())) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 656 | agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz], |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 657 | ca->num_frames_per_band(), split_rate_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 658 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 659 | RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 661 | if (synthesis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 662 | ca->MergeFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 665 | // TODO(aluebs): Investigate if the transient suppression placement should be |
| 666 | // before or after the AGC. |
| 667 | if (transient_suppressor_enabled_) { |
| 668 | float voice_probability = |
| 669 | agc_manager_.get() ? agc_manager_->voice_probability() : 1.f; |
| 670 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 671 | transient_suppressor_->Suppress( |
| 672 | ca->channels_f()[0], ca->num_frames(), ca->num_channels(), |
| 673 | ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(), |
| 674 | ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability, |
| 675 | key_pressed_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 676 | } |
| 677 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 678 | // The level estimator operates on the recombined data. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 679 | RETURN_ON_ERR(level_estimator_->ProcessStream(ca)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 680 | |
andrew@webrtc.org | 1e91693 | 2011-11-29 18:28:57 +0000 | [diff] [blame] | 681 | was_stream_delay_set_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 682 | return kNoError; |
| 683 | } |
| 684 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 685 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
| 686 | int samples_per_channel, |
| 687 | int sample_rate_hz, |
| 688 | ChannelLayout layout) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 689 | const StreamConfig reverse_config = { |
| 690 | sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout), |
| 691 | }; |
| 692 | if (samples_per_channel != reverse_config.num_frames()) { |
| 693 | return kBadDataLengthError; |
| 694 | } |
| 695 | return AnalyzeReverseStream(data, reverse_config); |
| 696 | } |
| 697 | |
| 698 | int AudioProcessingImpl::AnalyzeReverseStream( |
| 699 | const float* const* data, |
| 700 | const StreamConfig& reverse_config) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 701 | CriticalSectionScoped crit_scoped(crit_); |
| 702 | if (data == NULL) { |
| 703 | return kNullPointerError; |
| 704 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 705 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 706 | if (reverse_config.num_channels() <= 0) { |
| 707 | return kBadNumberChannelsError; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 710 | ProcessingConfig processing_config = api_format_; |
| 711 | processing_config.reverse_stream() = reverse_config; |
| 712 | |
| 713 | RETURN_ON_ERR(MaybeInitializeLocked(processing_config)); |
| 714 | assert(reverse_config.num_frames() == |
| 715 | api_format_.reverse_stream().num_frames()); |
| 716 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 717 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 718 | if (debug_file_->Open()) { |
| 719 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 720 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 721 | const size_t channel_size = |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 722 | sizeof(float) * api_format_.reverse_stream().num_frames(); |
| 723 | for (int i = 0; i < api_format_.reverse_stream().num_channels(); ++i) |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 724 | msg->add_channel(data[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 725 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 726 | } |
| 727 | #endif |
| 728 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 729 | render_audio_->CopyFrom(data, api_format_.reverse_stream()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 730 | return AnalyzeReverseStreamLocked(); |
| 731 | } |
| 732 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 733 | int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 734 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | if (frame == NULL) { |
| 736 | return kNullPointerError; |
| 737 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 738 | // Must be a native rate. |
| 739 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 740 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 741 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 742 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 743 | return kBadSampleRateError; |
| 744 | } |
| 745 | // This interface does not tolerate different forward and reverse rates. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 746 | if (frame->sample_rate_hz_ != api_format_.input_stream().sample_rate_hz()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 747 | return kBadSampleRateError; |
| 748 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 749 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 750 | if (frame->num_channels_ <= 0) { |
| 751 | return kBadNumberChannelsError; |
| 752 | } |
| 753 | |
| 754 | ProcessingConfig processing_config = api_format_; |
| 755 | processing_config.reverse_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 756 | processing_config.reverse_stream().set_num_channels(frame->num_channels_); |
| 757 | |
| 758 | RETURN_ON_ERR(MaybeInitializeLocked(processing_config)); |
| 759 | if (frame->samples_per_channel_ != |
| 760 | api_format_.reverse_stream().num_frames()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 761 | return kBadDataLengthError; |
| 762 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 763 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 764 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 765 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 766 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 767 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 768 | const size_t data_size = |
| 769 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 770 | msg->set_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 771 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 772 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 773 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 774 | |
| 775 | render_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 776 | return AnalyzeReverseStreamLocked(); |
| 777 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 778 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 779 | int AudioProcessingImpl::AnalyzeReverseStreamLocked() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 780 | AudioBuffer* ra = render_audio_.get(); // For brevity. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 781 | if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 782 | ra->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 783 | } |
| 784 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 785 | RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra)); |
| 786 | RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 787 | if (!use_new_agc_) { |
| 788 | RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra)); |
| 789 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 790 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 791 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 795 | Error retval = kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 796 | was_stream_delay_set_ = true; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 797 | delay += delay_offset_ms_; |
| 798 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 799 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 800 | delay = 0; |
| 801 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 805 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 806 | delay = 500; |
| 807 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | stream_delay_ms_ = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 811 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | int AudioProcessingImpl::stream_delay_ms() const { |
| 815 | return stream_delay_ms_; |
| 816 | } |
| 817 | |
| 818 | bool AudioProcessingImpl::was_stream_delay_set() const { |
| 819 | return was_stream_delay_set_; |
| 820 | } |
| 821 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 822 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
| 823 | key_pressed_ = key_pressed; |
| 824 | } |
| 825 | |
| 826 | bool AudioProcessingImpl::stream_key_pressed() const { |
| 827 | return key_pressed_; |
| 828 | } |
| 829 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 830 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
| 831 | CriticalSectionScoped crit_scoped(crit_); |
| 832 | delay_offset_ms_ = offset; |
| 833 | } |
| 834 | |
| 835 | int AudioProcessingImpl::delay_offset_ms() const { |
| 836 | return delay_offset_ms_; |
| 837 | } |
| 838 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 839 | int AudioProcessingImpl::StartDebugRecording( |
| 840 | const char filename[AudioProcessing::kMaxFilenameSize]) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 841 | CriticalSectionScoped crit_scoped(crit_); |
André Susano Pinto | 664cdaf | 2015-05-20 11:11:07 +0200 | [diff] [blame] | 842 | static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, ""); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 843 | |
| 844 | if (filename == NULL) { |
| 845 | return kNullPointerError; |
| 846 | } |
| 847 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 848 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 849 | // Stop any ongoing recording. |
| 850 | if (debug_file_->Open()) { |
| 851 | if (debug_file_->CloseFile() == -1) { |
| 852 | return kFileError; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | if (debug_file_->OpenFile(filename, false) == -1) { |
| 857 | debug_file_->CloseFile(); |
| 858 | return kFileError; |
| 859 | } |
| 860 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 861 | int err = WriteInitMessage(); |
| 862 | if (err != kNoError) { |
| 863 | return err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 864 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 865 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 866 | #else |
| 867 | return kUnsupportedFunctionError; |
| 868 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 869 | } |
| 870 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 871 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 872 | CriticalSectionScoped crit_scoped(crit_); |
| 873 | |
| 874 | if (handle == NULL) { |
| 875 | return kNullPointerError; |
| 876 | } |
| 877 | |
| 878 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 879 | // Stop any ongoing recording. |
| 880 | if (debug_file_->Open()) { |
| 881 | if (debug_file_->CloseFile() == -1) { |
| 882 | return kFileError; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) { |
| 887 | return kFileError; |
| 888 | } |
| 889 | |
| 890 | int err = WriteInitMessage(); |
| 891 | if (err != kNoError) { |
| 892 | return err; |
| 893 | } |
| 894 | return kNoError; |
| 895 | #else |
| 896 | return kUnsupportedFunctionError; |
| 897 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 898 | } |
| 899 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 900 | int AudioProcessingImpl::StartDebugRecordingForPlatformFile( |
| 901 | rtc::PlatformFile handle) { |
| 902 | FILE* stream = rtc::FdopenPlatformFileForWriting(handle); |
| 903 | return StartDebugRecording(stream); |
| 904 | } |
| 905 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 906 | int AudioProcessingImpl::StopDebugRecording() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 907 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 908 | |
| 909 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 910 | // We just return if recording hasn't started. |
| 911 | if (debug_file_->Open()) { |
| 912 | if (debug_file_->CloseFile() == -1) { |
| 913 | return kFileError; |
| 914 | } |
| 915 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 916 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 917 | #else |
| 918 | return kUnsupportedFunctionError; |
| 919 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
| 923 | return echo_cancellation_; |
| 924 | } |
| 925 | |
| 926 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
| 927 | return echo_control_mobile_; |
| 928 | } |
| 929 | |
| 930 | GainControl* AudioProcessingImpl::gain_control() const { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 931 | if (use_new_agc_) { |
| 932 | return gain_control_for_new_agc_.get(); |
| 933 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 934 | return gain_control_; |
| 935 | } |
| 936 | |
| 937 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
| 938 | return high_pass_filter_; |
| 939 | } |
| 940 | |
| 941 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
| 942 | return level_estimator_; |
| 943 | } |
| 944 | |
| 945 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
| 946 | return noise_suppression_; |
| 947 | } |
| 948 | |
| 949 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
| 950 | return voice_detection_; |
| 951 | } |
| 952 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 953 | bool AudioProcessingImpl::is_data_processed() const { |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 954 | if (beamformer_enabled_) { |
| 955 | return true; |
| 956 | } |
| 957 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 958 | int enabled_count = 0; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 959 | for (auto item : component_list_) { |
| 960 | if (item->is_component_enabled()) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 961 | enabled_count++; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // Data is unchanged if no components are enabled, or if only level_estimator_ |
| 966 | // or voice_detection_ is enabled. |
| 967 | if (enabled_count == 0) { |
| 968 | return false; |
| 969 | } else if (enabled_count == 1) { |
| 970 | if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) { |
| 971 | return false; |
| 972 | } |
| 973 | } else if (enabled_count == 2) { |
| 974 | if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) { |
| 975 | return false; |
| 976 | } |
| 977 | } |
| 978 | return true; |
| 979 | } |
| 980 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 981 | bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const { |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 982 | // Check if we've upmixed or downmixed the audio. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 983 | return ((api_format_.output_stream().num_channels() != |
| 984 | api_format_.input_stream().num_channels()) || |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 985 | is_data_processed || transient_suppressor_enabled_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 986 | } |
| 987 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 988 | bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 989 | return (is_data_processed && |
| 990 | (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz || |
| 991 | fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz)); |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 995 | if (!is_data_processed && !voice_detection_->is_enabled() && |
| 996 | !transient_suppressor_enabled_) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 997 | // Only level_estimator_ is enabled. |
| 998 | return false; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 999 | } else if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz || |
| 1000 | fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1001 | // Something besides level_estimator_ is enabled, and we have super-wb. |
| 1002 | return true; |
| 1003 | } |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 1007 | void AudioProcessingImpl::InitializeExperimentalAgc() { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1008 | if (use_new_agc_) { |
| 1009 | if (!agc_manager_.get()) { |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 1010 | agc_manager_.reset(new AgcManagerDirect(gain_control_, |
| 1011 | gain_control_for_new_agc_.get(), |
| 1012 | agc_startup_min_volume_)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1013 | } |
| 1014 | agc_manager_->Initialize(); |
| 1015 | agc_manager_->SetCaptureMuted(output_will_be_muted_); |
| 1016 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 1019 | void AudioProcessingImpl::InitializeTransient() { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1020 | if (transient_suppressor_enabled_) { |
| 1021 | if (!transient_suppressor_.get()) { |
| 1022 | transient_suppressor_.reset(new TransientSuppressor()); |
| 1023 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1024 | transient_suppressor_->Initialize( |
| 1025 | fwd_proc_format_.sample_rate_hz(), split_rate_, |
| 1026 | api_format_.output_stream().num_channels()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1027 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1030 | void AudioProcessingImpl::InitializeBeamformer() { |
| 1031 | if (beamformer_enabled_) { |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 1032 | if (!beamformer_) { |
mgraczyk@chromium.org | 0f663de | 2015-03-13 00:13:32 +0000 | [diff] [blame] | 1033 | beamformer_.reset(new NonlinearBeamformer(array_geometry_)); |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 1034 | } |
| 1035 | beamformer_->Initialize(kChunkSizeMs, split_rate_); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1039 | void AudioProcessingImpl::MaybeUpdateHistograms() { |
Bjorn Volcker | d92f267 | 2015-07-05 10:46:01 +0200 | [diff] [blame] | 1040 | static const int kMinDiffDelayMs = 60; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1041 | |
| 1042 | if (echo_cancellation()->is_enabled()) { |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1043 | // Activate delay_jumps_ counters if we know echo_cancellation is runnning. |
| 1044 | // If a stream has echo we know that the echo_cancellation is in process. |
| 1045 | if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) { |
| 1046 | stream_delay_jumps_ = 0; |
| 1047 | } |
| 1048 | if (aec_system_delay_jumps_ == -1 && |
| 1049 | echo_cancellation()->stream_has_echo()) { |
| 1050 | aec_system_delay_jumps_ = 0; |
| 1051 | } |
| 1052 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1053 | // Detect a jump in platform reported system delay and log the difference. |
| 1054 | const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_; |
| 1055 | if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) { |
| 1056 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump", |
| 1057 | diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1058 | if (stream_delay_jumps_ == -1) { |
| 1059 | stream_delay_jumps_ = 0; // Activate counter if needed. |
| 1060 | } |
| 1061 | stream_delay_jumps_++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1062 | } |
| 1063 | last_stream_delay_ms_ = stream_delay_ms_; |
| 1064 | |
| 1065 | // Detect a jump in AEC system delay and log the difference. |
| 1066 | const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000); |
| 1067 | const int aec_system_delay_ms = |
| 1068 | WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1069 | const int diff_aec_system_delay_ms = |
| 1070 | aec_system_delay_ms - last_aec_system_delay_ms_; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1071 | if (diff_aec_system_delay_ms > kMinDiffDelayMs && |
| 1072 | last_aec_system_delay_ms_ != 0) { |
| 1073 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump", |
| 1074 | diff_aec_system_delay_ms, kMinDiffDelayMs, 1000, |
| 1075 | 100); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1076 | if (aec_system_delay_jumps_ == -1) { |
| 1077 | aec_system_delay_jumps_ = 0; // Activate counter if needed. |
| 1078 | } |
| 1079 | aec_system_delay_jumps_++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1080 | } |
| 1081 | last_aec_system_delay_ms_ = aec_system_delay_ms; |
| 1082 | } |
| 1083 | } |
| 1084 | |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1085 | void AudioProcessingImpl::UpdateHistogramsOnCallEnd() { |
| 1086 | CriticalSectionScoped crit_scoped(crit_); |
| 1087 | if (stream_delay_jumps_ > -1) { |
| 1088 | RTC_HISTOGRAM_ENUMERATION( |
| 1089 | "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps", |
| 1090 | stream_delay_jumps_, 51); |
| 1091 | } |
| 1092 | stream_delay_jumps_ = -1; |
| 1093 | last_stream_delay_ms_ = 0; |
| 1094 | |
| 1095 | if (aec_system_delay_jumps_ > -1) { |
| 1096 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps", |
| 1097 | aec_system_delay_jumps_, 51); |
| 1098 | } |
| 1099 | aec_system_delay_jumps_ = -1; |
| 1100 | last_aec_system_delay_ms_ = 0; |
| 1101 | } |
| 1102 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1103 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1104 | int AudioProcessingImpl::WriteMessageToDebugFile() { |
| 1105 | int32_t size = event_msg_->ByteSize(); |
| 1106 | if (size <= 0) { |
| 1107 | return kUnspecifiedError; |
| 1108 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 1109 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1110 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 1111 | // pretty safe in assuming little-endian. |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1112 | #endif |
| 1113 | |
| 1114 | if (!event_msg_->SerializeToString(&event_str_)) { |
| 1115 | return kUnspecifiedError; |
| 1116 | } |
| 1117 | |
| 1118 | // Write message preceded by its size. |
| 1119 | if (!debug_file_->Write(&size, sizeof(int32_t))) { |
| 1120 | return kFileError; |
| 1121 | } |
| 1122 | if (!debug_file_->Write(event_str_.data(), event_str_.length())) { |
| 1123 | return kFileError; |
| 1124 | } |
| 1125 | |
| 1126 | event_msg_->Clear(); |
| 1127 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1128 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | int AudioProcessingImpl::WriteInitMessage() { |
| 1132 | event_msg_->set_type(audioproc::Event::INIT); |
| 1133 | audioproc::Init* msg = event_msg_->mutable_init(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1134 | msg->set_sample_rate(api_format_.input_stream().sample_rate_hz()); |
| 1135 | msg->set_num_input_channels(api_format_.input_stream().num_channels()); |
| 1136 | msg->set_num_output_channels(api_format_.output_stream().num_channels()); |
| 1137 | msg->set_num_reverse_channels(api_format_.reverse_stream().num_channels()); |
| 1138 | msg->set_reverse_sample_rate(api_format_.reverse_stream().sample_rate_hz()); |
| 1139 | msg->set_output_sample_rate(api_format_.output_stream().sample_rate_hz()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1140 | |
| 1141 | int err = WriteMessageToDebugFile(); |
| 1142 | if (err != kNoError) { |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
| 1146 | return kNoError; |
| 1147 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1148 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1149 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1150 | } // namespace webrtc |