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