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