niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +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 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 15 | #include <memory> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 16 | #include <vector> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
Henrik Kjellander | dca1e09 | 2017-07-01 16:42:22 +0200 | [diff] [blame] | 18 | #include "webrtc/base/criticalsection.h" |
| 19 | #include "webrtc/base/function_view.h" |
| 20 | #include "webrtc/base/gtest_prod_util.h" |
| 21 | #include "webrtc/base/ignore_wundef.h" |
| 22 | #include "webrtc/base/protobuf_utils.h" |
| 23 | #include "webrtc/base/swap_queue.h" |
| 24 | #include "webrtc/base/thread_annotations.h" |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 25 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 26 | #include "webrtc/modules/audio_processing/include/aec_dump.h" |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 27 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 28 | #include "webrtc/modules/audio_processing/render_queue_item_verifier.h" |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 29 | #include "webrtc/modules/audio_processing/rms_level.h" |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 30 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
| 31 | |
| 32 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 33 | // *.pb.h files are generated at build-time by the protobuf compiler. |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 34 | RTC_PUSH_IGNORING_WUNDEF() |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 35 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 36 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
| 37 | #else |
kjellander | 78ddd73 | 2016-02-09 08:13:06 -0800 | [diff] [blame] | 38 | #include "webrtc/modules/audio_processing/debug.pb.h" |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 39 | #endif |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 40 | RTC_POP_IGNORING_WUNDEF() |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 41 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
| 43 | namespace webrtc { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 44 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 45 | class AudioConverter; |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 46 | class NonlinearBeamformer; |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 47 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | class AudioProcessingImpl : public AudioProcessing { |
| 49 | public: |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 50 | // Methods forcing APM to run in a single-threaded manner. |
| 51 | // Acquires both the render and capture locks. |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 52 | explicit AudioProcessingImpl(const webrtc::Config& config); |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 53 | // AudioProcessingImpl takes ownership of beamformer. |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 54 | AudioProcessingImpl(const webrtc::Config& config, |
| 55 | NonlinearBeamformer* beamformer); |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 56 | ~AudioProcessingImpl() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 57 | int Initialize() override; |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 58 | int Initialize(int capture_input_sample_rate_hz, |
| 59 | int capture_output_sample_rate_hz, |
| 60 | int render_sample_rate_hz, |
| 61 | ChannelLayout capture_input_layout, |
| 62 | ChannelLayout capture_output_layout, |
| 63 | ChannelLayout render_input_layout) override; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 64 | int Initialize(const ProcessingConfig& processing_config) override; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 65 | void ApplyConfig(const AudioProcessing::Config& config) override; |
| 66 | void SetExtraOptions(const webrtc::Config& config) override; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 67 | void UpdateHistogramsOnCallEnd() override; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 68 | void AttachAecDump(std::unique_ptr<AecDump> aec_dump) override; |
| 69 | void DetachAecDump() override; |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 70 | int StartDebugRecording(const char filename[kMaxFilenameSize], |
| 71 | int64_t max_log_size_bytes) override; |
| 72 | int StartDebugRecording(FILE* handle, int64_t max_log_size_bytes) override; |
peah | 73a28ee | 2016-10-12 03:01:49 -0700 | [diff] [blame] | 73 | int StartDebugRecording(FILE* handle) override; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 74 | int StartDebugRecordingForPlatformFile(rtc::PlatformFile handle) override; |
| 75 | int StopDebugRecording() override; |
| 76 | |
| 77 | // Capture-side exclusive methods possibly running APM in a |
| 78 | // multi-threaded manner. Acquire the capture lock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 79 | int ProcessStream(AudioFrame* frame) override; |
| 80 | int ProcessStream(const float* const* src, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | size_t samples_per_channel, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 82 | int input_sample_rate_hz, |
| 83 | ChannelLayout input_layout, |
| 84 | int output_sample_rate_hz, |
| 85 | ChannelLayout output_layout, |
| 86 | float* const* dest) override; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 87 | int ProcessStream(const float* const* src, |
| 88 | const StreamConfig& input_config, |
| 89 | const StreamConfig& output_config, |
| 90 | float* const* dest) override; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 91 | void set_output_will_be_muted(bool muted) override; |
| 92 | int set_stream_delay_ms(int delay) override; |
| 93 | void set_delay_offset_ms(int offset) override; |
| 94 | int delay_offset_ms() const override; |
| 95 | void set_stream_key_pressed(bool key_pressed) override; |
| 96 | |
| 97 | // Render-side exclusive methods possibly running APM in a |
| 98 | // multi-threaded manner. Acquire the render lock. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 99 | int ProcessReverseStream(AudioFrame* frame) override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 100 | int AnalyzeReverseStream(const float* const* data, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 101 | size_t samples_per_channel, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 102 | int sample_rate_hz, |
| 103 | ChannelLayout layout) override; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 104 | int ProcessReverseStream(const float* const* src, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 105 | const StreamConfig& input_config, |
| 106 | const StreamConfig& output_config, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 107 | float* const* dest) override; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 108 | |
| 109 | // Methods only accessed from APM submodules or |
| 110 | // from AudioProcessing tests in a single-threaded manner. |
| 111 | // Hence there is no need for locks in these. |
| 112 | int proc_sample_rate_hz() const override; |
| 113 | int proc_split_sample_rate_hz() const override; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 114 | size_t num_input_channels() const override; |
| 115 | size_t num_proc_channels() const override; |
| 116 | size_t num_output_channels() const override; |
| 117 | size_t num_reverse_channels() const override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 118 | int stream_delay_ms() const override; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 119 | bool was_stream_delay_set() const override |
| 120 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 121 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 122 | AudioProcessingStatistics GetStatistics() const override; |
| 123 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 124 | // Methods returning pointers to APM submodules. |
| 125 | // No locks are aquired in those, as those locks |
| 126 | // would offer no protection (the submodules are |
| 127 | // created only once in a single-treaded manner |
| 128 | // during APM creation). |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 129 | EchoCancellation* echo_cancellation() const override; |
| 130 | EchoControlMobile* echo_control_mobile() const override; |
| 131 | GainControl* gain_control() const override; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 132 | // TODO(peah): Deprecate this API call. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 133 | HighPassFilter* high_pass_filter() const override; |
| 134 | LevelEstimator* level_estimator() const override; |
| 135 | NoiseSuppression* noise_suppression() const override; |
| 136 | VoiceDetection* voice_detection() const override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 138 | // TODO(peah): Remove MutateConfig once the new API allows that. |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 139 | void MutateConfig(rtc::FunctionView<void(AudioProcessing::Config*)> mutator); |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 140 | AudioProcessing::Config GetConfig() const override; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 141 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 142 | protected: |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 143 | // Overridden in a mock. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 144 | virtual int InitializeLocked() |
| 145 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 146 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | private: |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 148 | // TODO(peah): These friend classes should be removed as soon as the new |
| 149 | // parameter setting scheme allows. |
| 150 | FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, DefaultBehavior); |
| 151 | FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, ValidConfigBehavior); |
| 152 | FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, InValidConfigBehavior); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 153 | struct ApmPublicSubmodules; |
| 154 | struct ApmPrivateSubmodules; |
| 155 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 156 | // Submodule interface implementations. |
| 157 | std::unique_ptr<HighPassFilter> high_pass_filter_impl_; |
| 158 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 159 | class ApmSubmoduleStates { |
| 160 | public: |
| 161 | ApmSubmoduleStates(); |
| 162 | // Updates the submodule state and returns true if it has changed. |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 163 | bool Update(bool low_cut_filter_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 164 | bool echo_canceller_enabled, |
| 165 | bool mobile_echo_controller_enabled, |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 166 | bool residual_echo_detector_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 167 | bool noise_suppressor_enabled, |
| 168 | bool intelligibility_enhancer_enabled, |
| 169 | bool beamformer_enabled, |
| 170 | bool adaptive_gain_controller_enabled, |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 171 | bool gain_controller2_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 172 | bool level_controller_enabled, |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 173 | bool echo_canceller3_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 174 | bool voice_activity_detector_enabled, |
| 175 | bool level_estimator_enabled, |
| 176 | bool transient_suppressor_enabled); |
| 177 | bool CaptureMultiBandSubModulesActive() const; |
| 178 | bool CaptureMultiBandProcessingActive() const; |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame] | 179 | bool CaptureFullBandProcessingActive() const; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 180 | bool RenderMultiBandSubModulesActive() const; |
| 181 | bool RenderMultiBandProcessingActive() const; |
| 182 | |
| 183 | private: |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 184 | bool low_cut_filter_enabled_ = false; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 185 | bool echo_canceller_enabled_ = false; |
| 186 | bool mobile_echo_controller_enabled_ = false; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 187 | bool residual_echo_detector_enabled_ = false; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 188 | bool noise_suppressor_enabled_ = false; |
| 189 | bool intelligibility_enhancer_enabled_ = false; |
| 190 | bool beamformer_enabled_ = false; |
| 191 | bool adaptive_gain_controller_enabled_ = false; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 192 | bool gain_controller2_enabled_ = false; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 193 | bool level_controller_enabled_ = false; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 194 | bool echo_canceller3_enabled_ = false; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 195 | bool level_estimator_enabled_ = false; |
| 196 | bool voice_activity_detector_enabled_ = false; |
| 197 | bool transient_suppressor_enabled_ = false; |
| 198 | bool first_update_ = true; |
| 199 | }; |
| 200 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 201 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 202 | // State for the debug dump. |
| 203 | struct ApmDebugDumpThreadState { |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 204 | ApmDebugDumpThreadState(); |
| 205 | ~ApmDebugDumpThreadState(); |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 206 | std::unique_ptr<audioproc::Event> event_msg; // Protobuf message. |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 207 | ProtoString event_str; // Memory for protobuf serialization. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 208 | |
| 209 | // Serialized string of last saved APM configuration. |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 210 | ProtoString last_serialized_config; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct ApmDebugDumpState { |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 214 | ApmDebugDumpState(); |
| 215 | ~ApmDebugDumpState(); |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 216 | // Number of bytes that can still be written to the log before the maximum |
| 217 | // size is reached. A value of <= 0 indicates that no limit is used. |
| 218 | int64_t num_bytes_left_for_log_ = -1; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 219 | std::unique_ptr<FileWrapper> debug_file; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 220 | ApmDebugDumpThreadState render; |
| 221 | ApmDebugDumpThreadState capture; |
| 222 | }; |
| 223 | #endif |
| 224 | |
| 225 | // Method for modifying the formats struct that are called from both |
| 226 | // the render and capture threads. The check for whether modifications |
| 227 | // are needed is done while holding the render lock only, thereby avoiding |
| 228 | // that the capture thread blocks the render thread. |
| 229 | // The struct is modified in a single-threaded manner by holding both the |
| 230 | // render and capture locks. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 231 | int MaybeInitialize(const ProcessingConfig& config, bool force_initialization) |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 232 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
| 233 | |
| 234 | int MaybeInitializeRender(const ProcessingConfig& processing_config) |
| 235 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
| 236 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 237 | int MaybeInitializeCapture(const ProcessingConfig& processing_config, |
| 238 | bool force_initialization) |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 239 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
| 240 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 241 | // Method for updating the state keeping track of the active submodules. |
| 242 | // Returns a bool indicating whether the state has changed. |
| 243 | bool UpdateActiveSubmoduleStates() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 244 | |
| 245 | // Methods requiring APM running in a single-threaded manner. |
| 246 | // Are called with both the render and capture locks already |
| 247 | // acquired. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 248 | void InitializeTransient() |
| 249 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
| 250 | void InitializeBeamformer() |
| 251 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
| 252 | void InitializeIntelligibility() |
| 253 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 254 | int InitializeLocked(const ProcessingConfig& config) |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 255 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 256 | void InitializeLevelController() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 257 | void InitializeResidualEchoDetector() |
| 258 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 259 | void InitializeLowCutFilter() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 260 | void InitializeEchoCanceller3() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 261 | void InitializeGainController2(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 262 | |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 263 | void EmptyQueuedRenderAudio(); |
| 264 | void AllocateRenderQueue() |
| 265 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 266 | void QueueBandedRenderAudio(AudioBuffer* audio) |
| 267 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
| 268 | void QueueNonbandedRenderAudio(AudioBuffer* audio) |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 269 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
| 270 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 271 | // Capture-side exclusive methods possibly running APM in a multi-threaded |
| 272 | // manner that are called with the render lock already acquired. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 273 | int ProcessCaptureStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 274 | void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 275 | |
| 276 | // Render-side exclusive methods possibly running APM in a multi-threaded |
| 277 | // manner that are called with the render lock already acquired. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 278 | // TODO(ekm): Remove once all clients updated to new interface. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 279 | int AnalyzeReverseStreamLocked(const float* const* src, |
| 280 | const StreamConfig& input_config, |
| 281 | const StreamConfig& output_config) |
| 282 | EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 283 | int ProcessRenderStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_render_); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 284 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 285 | // Collects configuration settings from public and private |
| 286 | // submodules to be saved as an audioproc::Config message on the |
| 287 | // AecDump if it is attached. If not |forced|, only writes the current |
| 288 | // config if it is different from the last saved one; if |forced|, |
| 289 | // writes the config regardless of the last saved. |
| 290 | void WriteAecDumpConfigMessage(bool forced) |
| 291 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 292 | |
| 293 | // Notifies attached AecDump of current configuration and capture data. |
| 294 | void RecordUnprocessedCaptureStream(const float* const* capture_stream) |
| 295 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 296 | |
| 297 | void RecordUnprocessedCaptureStream(const AudioFrame& capture_frame) |
| 298 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 299 | |
| 300 | // Notifies attached AecDump of current configuration and |
| 301 | // processed capture data and issues a capture stream recording |
| 302 | // request. |
| 303 | void RecordProcessedCaptureStream( |
| 304 | const float* const* processed_capture_stream) |
| 305 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 306 | |
| 307 | void RecordProcessedCaptureStream(const AudioFrame& processed_capture_frame) |
| 308 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 309 | |
| 310 | // Notifies attached AecDump about current state (delay, drift, etc). |
| 311 | void RecordAudioProcessingState() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
| 312 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 313 | // Debug dump methods that are internal and called without locks. |
| 314 | // TODO(peah): Make thread safe. |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 315 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 316 | // TODO(andrew): make this more graceful. Ideally we would split this stuff |
| 317 | // out into a separate class with an "enabled" and "disabled" implementation. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 318 | static int WriteMessageToDebugFile(FileWrapper* debug_file, |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 319 | int64_t* filesize_limit_bytes, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 320 | rtc::CriticalSection* crit_debug, |
| 321 | ApmDebugDumpThreadState* debug_state); |
| 322 | int WriteInitMessage() EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 323 | |
| 324 | // Writes Config message. If not |forced|, only writes the current config if |
| 325 | // it is different from the last saved one; if |forced|, writes the config |
| 326 | // regardless of the last saved. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 327 | int WriteConfigMessage(bool forced) EXCLUSIVE_LOCKS_REQUIRED(crit_capture_) |
| 328 | EXCLUSIVE_LOCKS_REQUIRED(crit_capture_); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 329 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 330 | // Critical section. |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 331 | rtc::CriticalSection crit_debug_; |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 332 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 333 | // Debug dump state. |
| 334 | ApmDebugDumpState debug_dump_; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 335 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 337 | // AecDump instance used for optionally logging APM config, input |
| 338 | // and output to file in the AEC-dump format defined in debug.proto. |
| 339 | std::unique_ptr<AecDump> aec_dump_; |
| 340 | |
| 341 | // Hold the last config written with AecDump for avoiding writing |
| 342 | // the same config twice. |
| 343 | InternalAPMConfig apm_config_for_aec_dump_ GUARDED_BY(crit_capture_); |
| 344 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 345 | // Critical sections. |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 346 | rtc::CriticalSection crit_render_ ACQUIRED_BEFORE(crit_capture_); |
| 347 | rtc::CriticalSection crit_capture_; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 348 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 349 | // Struct containing the Config specifying the behavior of APM. |
| 350 | AudioProcessing::Config config_; |
| 351 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 352 | // Class containing information about what submodules are active. |
| 353 | ApmSubmoduleStates submodule_states_; |
| 354 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 355 | // Structs containing the pointers to the submodules. |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 356 | std::unique_ptr<ApmPublicSubmodules> public_submodules_; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 357 | std::unique_ptr<ApmPrivateSubmodules> private_submodules_; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 358 | |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 359 | // State that is written to while holding both the render and capture locks |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 360 | // but can be read without any lock being held. |
| 361 | // As this is only accessed internally of APM, and all internal methods in APM |
| 362 | // either are holding the render or capture locks, this construct is safe as |
| 363 | // it is not possible to read the variables while writing them. |
| 364 | struct ApmFormatState { |
| 365 | ApmFormatState() |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 366 | : // Format of processing streams at input/output call sites. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 367 | api_format({{{kSampleRate16kHz, 1, false}, |
| 368 | {kSampleRate16kHz, 1, false}, |
| 369 | {kSampleRate16kHz, 1, false}, |
| 370 | {kSampleRate16kHz, 1, false}}}), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 371 | render_processing_format(kSampleRate16kHz, 1) {} |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 372 | ProcessingConfig api_format; |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 373 | StreamConfig render_processing_format; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 374 | } formats_; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 375 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 376 | // APM constants. |
| 377 | const struct ApmConstants { |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 378 | ApmConstants(int agc_startup_min_volume, |
| 379 | int agc_clipped_level_min, |
| 380 | bool use_experimental_agc) |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 381 | : // Format of processing streams at input/output call sites. |
| 382 | agc_startup_min_volume(agc_startup_min_volume), |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 383 | agc_clipped_level_min(agc_clipped_level_min), |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 384 | use_experimental_agc(use_experimental_agc) {} |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 385 | int agc_startup_min_volume; |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 386 | int agc_clipped_level_min; |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 387 | bool use_experimental_agc; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 388 | } constants_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 389 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 390 | struct ApmCaptureState { |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 391 | ApmCaptureState(bool transient_suppressor_enabled, |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 392 | const std::vector<Point>& array_geometry, |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 393 | SphericalPointf target_direction); |
| 394 | ~ApmCaptureState(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 395 | int aec_system_delay_jumps; |
| 396 | int delay_offset_ms; |
| 397 | bool was_stream_delay_set; |
| 398 | int last_stream_delay_ms; |
| 399 | int last_aec_system_delay_ms; |
| 400 | int stream_delay_jumps; |
| 401 | bool output_will_be_muted; |
| 402 | bool key_pressed; |
| 403 | bool transient_suppressor_enabled; |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 404 | std::vector<Point> array_geometry; |
| 405 | SphericalPointf target_direction; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 406 | std::unique_ptr<AudioBuffer> capture_audio; |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 407 | // Only the rate and samples fields of capture_processing_format_ are used |
| 408 | // because the capture processing number of channels is mutable and is |
| 409 | // tracked by the capture_audio_. |
| 410 | StreamConfig capture_processing_format; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 411 | int split_rate; |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 412 | bool echo_path_gain_change; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 413 | } capture_ GUARDED_BY(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 414 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 415 | struct ApmCaptureNonLockedState { |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 416 | ApmCaptureNonLockedState(bool beamformer_enabled, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 417 | bool intelligibility_enabled) |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 418 | : capture_processing_format(kSampleRate16kHz), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 419 | split_rate(kSampleRate16kHz), |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 420 | stream_delay_ms(0), |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 421 | beamformer_enabled(beamformer_enabled), |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 422 | intelligibility_enabled(intelligibility_enabled) {} |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 423 | // Only the rate and samples fields of capture_processing_format_ are used |
| 424 | // because the forward processing number of channels is mutable and is |
| 425 | // tracked by the capture_audio_. |
| 426 | StreamConfig capture_processing_format; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 427 | int split_rate; |
| 428 | int stream_delay_ms; |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 429 | bool beamformer_enabled; |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 430 | bool intelligibility_enabled; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 431 | bool level_controller_enabled = false; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 432 | bool echo_canceller3_enabled = false; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 433 | bool gain_controller2_enabled = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 434 | } capture_nonlocked_; |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 435 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 436 | struct ApmRenderState { |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 437 | ApmRenderState(); |
| 438 | ~ApmRenderState(); |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 439 | std::unique_ptr<AudioConverter> render_converter; |
| 440 | std::unique_ptr<AudioBuffer> render_audio; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 441 | } render_ GUARDED_BY(crit_render_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 442 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 443 | size_t aec_render_queue_element_max_size_ GUARDED_BY(crit_render_) |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 444 | GUARDED_BY(crit_capture_) = 0; |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 445 | std::vector<float> aec_render_queue_buffer_ GUARDED_BY(crit_render_); |
| 446 | std::vector<float> aec_capture_queue_buffer_ GUARDED_BY(crit_capture_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 447 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 448 | size_t aecm_render_queue_element_max_size_ GUARDED_BY(crit_render_) |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 449 | GUARDED_BY(crit_capture_) = 0; |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 450 | std::vector<int16_t> aecm_render_queue_buffer_ GUARDED_BY(crit_render_); |
| 451 | std::vector<int16_t> aecm_capture_queue_buffer_ GUARDED_BY(crit_capture_); |
| 452 | |
| 453 | size_t agc_render_queue_element_max_size_ GUARDED_BY(crit_render_) |
| 454 | GUARDED_BY(crit_capture_) = 0; |
| 455 | std::vector<int16_t> agc_render_queue_buffer_ GUARDED_BY(crit_render_); |
| 456 | std::vector<int16_t> agc_capture_queue_buffer_ GUARDED_BY(crit_capture_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 457 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 458 | size_t red_render_queue_element_max_size_ GUARDED_BY(crit_render_) |
| 459 | GUARDED_BY(crit_capture_) = 0; |
| 460 | std::vector<float> red_render_queue_buffer_ GUARDED_BY(crit_render_); |
| 461 | std::vector<float> red_capture_queue_buffer_ GUARDED_BY(crit_capture_); |
| 462 | |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 463 | RmsLevel capture_input_rms_ GUARDED_BY(crit_capture_); |
| 464 | RmsLevel capture_output_rms_ GUARDED_BY(crit_capture_); |
| 465 | int capture_rms_interval_counter_ GUARDED_BY(crit_capture_) = 0; |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 466 | |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 467 | // Lock protection not needed. |
| 468 | std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>> |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 469 | aec_render_signal_queue_; |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 470 | std::unique_ptr< |
| 471 | SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>> |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 472 | aecm_render_signal_queue_; |
| 473 | std::unique_ptr< |
| 474 | SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>> |
| 475 | agc_render_signal_queue_; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 476 | std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>> |
| 477 | red_render_signal_queue_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 479 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 480 | } // namespace webrtc |
| 481 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 482 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_ |