blob: 7fc81f8899df3219537bc51289fad6fbebc105c4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org788acd12014-12-15 09:41:24 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
14#include <list>
kwiberg88788ad2016-02-19 07:04:49 -080015#include <memory>
Michael Graczyk86c6d332015-07-23 11:41:39 -070016#include <vector>
niklase@google.com470e71d2011-07-07 08:21:25 +000017
peahdf3efa82015-11-28 12:35:15 -080018#include "webrtc/base/criticalsection.h"
peah8271d042016-11-22 07:24:52 -080019#include "webrtc/base/function_view.h"
peahc19f3122016-10-07 14:54:10 -070020#include "webrtc/base/gtest_prod_util.h"
kwiberg77eab702016-09-28 17:42:01 -070021#include "webrtc/base/ignore_wundef.h"
mbonadei7c2c8432017-04-07 00:59:12 -070022#include "webrtc/base/protobuf_utils.h"
peah764e3642016-10-22 05:04:30 -070023#include "webrtc/base/swap_queue.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024#include "webrtc/base/thread_annotations.h"
peahdf3efa82015-11-28 12:35:15 -080025#include "webrtc/modules/audio_processing/audio_buffer.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070026#include "webrtc/modules/audio_processing/include/audio_processing.h"
peah764e3642016-10-22 05:04:30 -070027#include "webrtc/modules/audio_processing/render_queue_item_verifier.h"
henrik.lundin290d43a2016-11-29 08:09:09 -080028#include "webrtc/modules/audio_processing/rms_level.h"
peahdf3efa82015-11-28 12:35:15 -080029#include "webrtc/system_wrappers/include/file_wrapper.h"
30
31#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
mbonadei7c2c8432017-04-07 00:59:12 -070032// *.pb.h files are generated at build-time by the protobuf compiler.
kwiberg77eab702016-09-28 17:42:01 -070033RTC_PUSH_IGNORING_WUNDEF()
peahdf3efa82015-11-28 12:35:15 -080034#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
35#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
36#else
kjellander78ddd732016-02-09 08:13:06 -080037#include "webrtc/modules/audio_processing/debug.pb.h"
peahdf3efa82015-11-28 12:35:15 -080038#endif
kwiberg77eab702016-09-28 17:42:01 -070039RTC_POP_IGNORING_WUNDEF()
peahdf3efa82015-11-28 12:35:15 -080040#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000041
42namespace webrtc {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000043
ekmeyerson60d9b332015-08-14 10:35:55 -070044class AudioConverter;
Alejandro Luebsf4022ff2016-07-01 17:19:09 -070045class NonlinearBeamformer;
Michael Graczykdfa36052015-03-25 16:37:27 -070046
niklase@google.com470e71d2011-07-07 08:21:25 +000047class AudioProcessingImpl : public AudioProcessing {
48 public:
peahdf3efa82015-11-28 12:35:15 -080049 // Methods forcing APM to run in a single-threaded manner.
50 // Acquires both the render and capture locks.
peah88ac8532016-09-12 16:47:25 -070051 explicit AudioProcessingImpl(const webrtc::Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070052 // AudioProcessingImpl takes ownership of beamformer.
peah88ac8532016-09-12 16:47:25 -070053 AudioProcessingImpl(const webrtc::Config& config,
54 NonlinearBeamformer* beamformer);
kwiberg83ffe452016-08-29 14:46:07 -070055 ~AudioProcessingImpl() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 int Initialize() override;
peahde65ddc2016-09-16 15:02:15 -070057 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 Graczyk86c6d332015-07-23 11:41:39 -070063 int Initialize(const ProcessingConfig& processing_config) override;
peah88ac8532016-09-12 16:47:25 -070064 void ApplyConfig(const AudioProcessing::Config& config) override;
65 void SetExtraOptions(const webrtc::Config& config) override;
peahdf3efa82015-11-28 12:35:15 -080066 void UpdateHistogramsOnCallEnd() override;
ivocd66b44d2016-01-15 03:06:36 -080067 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;
peah73a28ee2016-10-12 03:01:49 -070070 int StartDebugRecording(FILE* handle) override;
peahdf3efa82015-11-28 12:35:15 -080071 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.org14665ff2015-03-04 12:58:35 +000076 int ProcessStream(AudioFrame* frame) override;
77 int ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 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 Graczyk86c6d332015-07-23 11:41:39 -070084 int ProcessStream(const float* const* src,
85 const StreamConfig& input_config,
86 const StreamConfig& output_config,
87 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -080088 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.
ekmeyerson60d9b332015-08-14 10:35:55 -070096 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000097 int AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -070098 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 int sample_rate_hz,
100 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -0700101 int ProcessReverseStream(const float* const* src,
peahde65ddc2016-09-16 15:02:15 -0700102 const StreamConfig& input_config,
103 const StreamConfig& output_config,
ekmeyerson60d9b332015-08-14 10:35:55 -0700104 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -0800105
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 Kasting69558702016-01-12 16:26:35 -0800111 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.org14665ff2015-03-04 12:58:35 +0000115 int stream_delay_ms() const override;
peahdf3efa82015-11-28 12:35:15 -0800116 bool was_stream_delay_set() const override
117 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
118
ivoc3e9a5372016-10-28 07:55:33 -0700119 AudioProcessingStatistics GetStatistics() const override;
120
peahdf3efa82015-11-28 12:35:15 -0800121 // 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.org14665ff2015-03-04 12:58:35 +0000126 EchoCancellation* echo_cancellation() const override;
127 EchoControlMobile* echo_control_mobile() const override;
128 GainControl* gain_control() const override;
peah8271d042016-11-22 07:24:52 -0800129 // TODO(peah): Deprecate this API call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000130 HighPassFilter* high_pass_filter() const override;
131 LevelEstimator* level_estimator() const override;
132 NoiseSuppression* noise_suppression() const override;
133 VoiceDetection* voice_detection() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
henrik.lundinadf06352017-04-05 05:48:24 -0700135 // TODO(peah): Remove MutateConfig once the new API allows that.
peah8271d042016-11-22 07:24:52 -0800136 void MutateConfig(rtc::FunctionView<void(AudioProcessing::Config*)> mutator);
henrik.lundinadf06352017-04-05 05:48:24 -0700137 AudioProcessing::Config GetConfig() const override;
peah8271d042016-11-22 07:24:52 -0800138
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000139 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000140 // Overridden in a mock.
peahdf3efa82015-11-28 12:35:15 -0800141 virtual int InitializeLocked()
142 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000143
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 private:
peahc19f3122016-10-07 14:54:10 -0700145 // TODO(peah): These friend classes should be removed as soon as the new
146 // parameter setting scheme allows.
147 FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, DefaultBehavior);
148 FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, ValidConfigBehavior);
149 FRIEND_TEST_ALL_PREFIXES(ApmConfiguration, InValidConfigBehavior);
peahdf3efa82015-11-28 12:35:15 -0800150 struct ApmPublicSubmodules;
151 struct ApmPrivateSubmodules;
152
peah8271d042016-11-22 07:24:52 -0800153 // Submodule interface implementations.
154 std::unique_ptr<HighPassFilter> high_pass_filter_impl_;
155
peah2ace3f92016-09-10 04:42:27 -0700156 class ApmSubmoduleStates {
157 public:
158 ApmSubmoduleStates();
159 // Updates the submodule state and returns true if it has changed.
peah8271d042016-11-22 07:24:52 -0800160 bool Update(bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700161 bool echo_canceller_enabled,
162 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700163 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700164 bool noise_suppressor_enabled,
165 bool intelligibility_enhancer_enabled,
166 bool beamformer_enabled,
167 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700168 bool gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -0800170 bool echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -0700171 bool voice_activity_detector_enabled,
172 bool level_estimator_enabled,
173 bool transient_suppressor_enabled);
174 bool CaptureMultiBandSubModulesActive() const;
175 bool CaptureMultiBandProcessingActive() const;
176 bool RenderMultiBandSubModulesActive() const;
177 bool RenderMultiBandProcessingActive() const;
178
179 private:
peah8271d042016-11-22 07:24:52 -0800180 bool low_cut_filter_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700181 bool echo_canceller_enabled_ = false;
182 bool mobile_echo_controller_enabled_ = false;
ivoc9f4a4a02016-10-28 05:39:16 -0700183 bool residual_echo_detector_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700184 bool noise_suppressor_enabled_ = false;
185 bool intelligibility_enhancer_enabled_ = false;
186 bool beamformer_enabled_ = false;
187 bool adaptive_gain_controller_enabled_ = false;
alessiob3ec96df2017-05-22 06:57:06 -0700188 bool gain_controller2_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700189 bool level_controller_enabled_ = false;
peahe0eae3c2016-12-14 01:16:23 -0800190 bool echo_canceller3_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700191 bool level_estimator_enabled_ = false;
192 bool voice_activity_detector_enabled_ = false;
193 bool transient_suppressor_enabled_ = false;
194 bool first_update_ = true;
195 };
196
peahdf3efa82015-11-28 12:35:15 -0800197#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
198 // State for the debug dump.
199 struct ApmDebugDumpThreadState {
kwiberg83ffe452016-08-29 14:46:07 -0700200 ApmDebugDumpThreadState();
201 ~ApmDebugDumpThreadState();
kwiberg88788ad2016-02-19 07:04:49 -0800202 std::unique_ptr<audioproc::Event> event_msg; // Protobuf message.
mbonadei7c2c8432017-04-07 00:59:12 -0700203 ProtoString event_str; // Memory for protobuf serialization.
peahdf3efa82015-11-28 12:35:15 -0800204
205 // Serialized string of last saved APM configuration.
mbonadei7c2c8432017-04-07 00:59:12 -0700206 ProtoString last_serialized_config;
peahdf3efa82015-11-28 12:35:15 -0800207 };
208
209 struct ApmDebugDumpState {
kwiberg83ffe452016-08-29 14:46:07 -0700210 ApmDebugDumpState();
211 ~ApmDebugDumpState();
ivocd66b44d2016-01-15 03:06:36 -0800212 // Number of bytes that can still be written to the log before the maximum
213 // size is reached. A value of <= 0 indicates that no limit is used.
214 int64_t num_bytes_left_for_log_ = -1;
kwiberg88788ad2016-02-19 07:04:49 -0800215 std::unique_ptr<FileWrapper> debug_file;
peahdf3efa82015-11-28 12:35:15 -0800216 ApmDebugDumpThreadState render;
217 ApmDebugDumpThreadState capture;
218 };
219#endif
220
221 // Method for modifying the formats struct that are called from both
222 // the render and capture threads. The check for whether modifications
223 // are needed is done while holding the render lock only, thereby avoiding
224 // that the capture thread blocks the render thread.
225 // The struct is modified in a single-threaded manner by holding both the
226 // render and capture locks.
peah2ace3f92016-09-10 04:42:27 -0700227 int MaybeInitialize(const ProcessingConfig& config, bool force_initialization)
peahdf3efa82015-11-28 12:35:15 -0800228 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
229
230 int MaybeInitializeRender(const ProcessingConfig& processing_config)
231 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
232
peah2ace3f92016-09-10 04:42:27 -0700233 int MaybeInitializeCapture(const ProcessingConfig& processing_config,
234 bool force_initialization)
peahdf3efa82015-11-28 12:35:15 -0800235 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
236
peah2ace3f92016-09-10 04:42:27 -0700237 // Method for updating the state keeping track of the active submodules.
238 // Returns a bool indicating whether the state has changed.
239 bool UpdateActiveSubmoduleStates() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800240
241 // Methods requiring APM running in a single-threaded manner.
242 // Are called with both the render and capture locks already
243 // acquired.
peahdf3efa82015-11-28 12:35:15 -0800244 void InitializeTransient()
245 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
246 void InitializeBeamformer()
247 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
248 void InitializeIntelligibility()
249 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700250 int InitializeLocked(const ProcessingConfig& config)
peahdf3efa82015-11-28 12:35:15 -0800251 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peahca4cac72016-06-29 15:26:12 -0700252 void InitializeLevelController() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
ivoc9f4a4a02016-10-28 05:39:16 -0700253 void InitializeResidualEchoDetector()
254 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peah8271d042016-11-22 07:24:52 -0800255 void InitializeLowCutFilter() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahe0eae3c2016-12-14 01:16:23 -0800256 void InitializeEchoCanceller3() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
alessiob3ec96df2017-05-22 06:57:06 -0700257 void InitializeGainController2();
peahdf3efa82015-11-28 12:35:15 -0800258
peah764e3642016-10-22 05:04:30 -0700259 void EmptyQueuedRenderAudio();
260 void AllocateRenderQueue()
261 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peah9e6a2902017-05-15 07:19:21 -0700262 void QueueBandedRenderAudio(AudioBuffer* audio)
263 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
264 void QueueNonbandedRenderAudio(AudioBuffer* audio)
peah764e3642016-10-22 05:04:30 -0700265 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
266
peahdf3efa82015-11-28 12:35:15 -0800267 // Capture-side exclusive methods possibly running APM in a multi-threaded
268 // manner that are called with the render lock already acquired.
peahde65ddc2016-09-16 15:02:15 -0700269 int ProcessCaptureStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800270 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
271
272 // Render-side exclusive methods possibly running APM in a multi-threaded
273 // manner that are called with the render lock already acquired.
ekmeyerson60d9b332015-08-14 10:35:55 -0700274 // TODO(ekm): Remove once all clients updated to new interface.
peahdf3efa82015-11-28 12:35:15 -0800275 int AnalyzeReverseStreamLocked(const float* const* src,
276 const StreamConfig& input_config,
277 const StreamConfig& output_config)
278 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
peahde65ddc2016-09-16 15:02:15 -0700279 int ProcessRenderStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000280
peahdf3efa82015-11-28 12:35:15 -0800281// Debug dump methods that are internal and called without locks.
282// TODO(peah): Make thread safe.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000283#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
284 // TODO(andrew): make this more graceful. Ideally we would split this stuff
285 // out into a separate class with an "enabled" and "disabled" implementation.
peahdf3efa82015-11-28 12:35:15 -0800286 static int WriteMessageToDebugFile(FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -0800287 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -0800288 rtc::CriticalSection* crit_debug,
289 ApmDebugDumpThreadState* debug_state);
290 int WriteInitMessage() EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200291
292 // Writes Config message. If not |forced|, only writes the current config if
293 // it is different from the last saved one; if |forced|, writes the config
294 // regardless of the last saved.
peahdf3efa82015-11-28 12:35:15 -0800295 int WriteConfigMessage(bool forced) EXCLUSIVE_LOCKS_REQUIRED(crit_capture_)
296 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200297
peahdf3efa82015-11-28 12:35:15 -0800298 // Critical section.
pbos5ad935c2016-01-25 03:52:44 -0800299 rtc::CriticalSection crit_debug_;
Minyue13b96ba2015-10-03 00:39:14 +0200300
peahdf3efa82015-11-28 12:35:15 -0800301 // Debug dump state.
302 ApmDebugDumpState debug_dump_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000303#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000304
peahdf3efa82015-11-28 12:35:15 -0800305 // Critical sections.
pbos5ad935c2016-01-25 03:52:44 -0800306 rtc::CriticalSection crit_render_ ACQUIRED_BEFORE(crit_capture_);
307 rtc::CriticalSection crit_capture_;
peahdf3efa82015-11-28 12:35:15 -0800308
peahc19f3122016-10-07 14:54:10 -0700309 // Struct containing the Config specifying the behavior of APM.
310 AudioProcessing::Config config_;
311
peah2ace3f92016-09-10 04:42:27 -0700312 // Class containing information about what submodules are active.
313 ApmSubmoduleStates submodule_states_;
314
peahdf3efa82015-11-28 12:35:15 -0800315 // Structs containing the pointers to the submodules.
kwiberg88788ad2016-02-19 07:04:49 -0800316 std::unique_ptr<ApmPublicSubmodules> public_submodules_;
ivoc9f4a4a02016-10-28 05:39:16 -0700317 std::unique_ptr<ApmPrivateSubmodules> private_submodules_;
peahdf3efa82015-11-28 12:35:15 -0800318
peah192164e2015-11-17 02:16:45 -0800319 // State that is written to while holding both the render and capture locks
peahdf3efa82015-11-28 12:35:15 -0800320 // but can be read without any lock being held.
321 // As this is only accessed internally of APM, and all internal methods in APM
322 // either are holding the render or capture locks, this construct is safe as
323 // it is not possible to read the variables while writing them.
324 struct ApmFormatState {
325 ApmFormatState()
peah192164e2015-11-17 02:16:45 -0800326 : // Format of processing streams at input/output call sites.
peahdf3efa82015-11-28 12:35:15 -0800327 api_format({{{kSampleRate16kHz, 1, false},
328 {kSampleRate16kHz, 1, false},
329 {kSampleRate16kHz, 1, false},
330 {kSampleRate16kHz, 1, false}}}),
peahde65ddc2016-09-16 15:02:15 -0700331 render_processing_format(kSampleRate16kHz, 1) {}
peahdf3efa82015-11-28 12:35:15 -0800332 ProcessingConfig api_format;
peahde65ddc2016-09-16 15:02:15 -0700333 StreamConfig render_processing_format;
peahdf3efa82015-11-28 12:35:15 -0800334 } formats_;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700335
peahdf3efa82015-11-28 12:35:15 -0800336 // APM constants.
337 const struct ApmConstants {
henrik.lundinbd681b92016-12-05 09:08:42 -0800338 ApmConstants(int agc_startup_min_volume,
339 int agc_clipped_level_min,
340 bool use_experimental_agc)
peahdf3efa82015-11-28 12:35:15 -0800341 : // Format of processing streams at input/output call sites.
342 agc_startup_min_volume(agc_startup_min_volume),
henrik.lundinbd681b92016-12-05 09:08:42 -0800343 agc_clipped_level_min(agc_clipped_level_min),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700344 use_experimental_agc(use_experimental_agc) {}
peahdf3efa82015-11-28 12:35:15 -0800345 int agc_startup_min_volume;
henrik.lundinbd681b92016-12-05 09:08:42 -0800346 int agc_clipped_level_min;
peahbe615622016-02-13 16:40:47 -0800347 bool use_experimental_agc;
peahdf3efa82015-11-28 12:35:15 -0800348 } constants_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000349
peahdf3efa82015-11-28 12:35:15 -0800350 struct ApmCaptureState {
aluebs2a346882016-01-11 18:04:30 -0800351 ApmCaptureState(bool transient_suppressor_enabled,
aluebs2a346882016-01-11 18:04:30 -0800352 const std::vector<Point>& array_geometry,
kwiberg83ffe452016-08-29 14:46:07 -0700353 SphericalPointf target_direction);
354 ~ApmCaptureState();
peahdf3efa82015-11-28 12:35:15 -0800355 int aec_system_delay_jumps;
356 int delay_offset_ms;
357 bool was_stream_delay_set;
358 int last_stream_delay_ms;
359 int last_aec_system_delay_ms;
360 int stream_delay_jumps;
361 bool output_will_be_muted;
362 bool key_pressed;
363 bool transient_suppressor_enabled;
aluebs2a346882016-01-11 18:04:30 -0800364 std::vector<Point> array_geometry;
365 SphericalPointf target_direction;
kwiberg88788ad2016-02-19 07:04:49 -0800366 std::unique_ptr<AudioBuffer> capture_audio;
peahde65ddc2016-09-16 15:02:15 -0700367 // Only the rate and samples fields of capture_processing_format_ are used
368 // because the capture processing number of channels is mutable and is
369 // tracked by the capture_audio_.
370 StreamConfig capture_processing_format;
peahdf3efa82015-11-28 12:35:15 -0800371 int split_rate;
peah67995532017-04-10 14:12:41 -0700372 int previous_agc_level;
373 bool echo_path_gain_change;
peahdf3efa82015-11-28 12:35:15 -0800374 } capture_ GUARDED_BY(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
peahdf3efa82015-11-28 12:35:15 -0800376 struct ApmCaptureNonLockedState {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700377 ApmCaptureNonLockedState(bool beamformer_enabled,
peah88ac8532016-09-12 16:47:25 -0700378 bool intelligibility_enabled)
peahde65ddc2016-09-16 15:02:15 -0700379 : capture_processing_format(kSampleRate16kHz),
peahdf3efa82015-11-28 12:35:15 -0800380 split_rate(kSampleRate16kHz),
aluebsb2328d12016-01-11 20:32:29 -0800381 stream_delay_ms(0),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700382 beamformer_enabled(beamformer_enabled),
peah88ac8532016-09-12 16:47:25 -0700383 intelligibility_enabled(intelligibility_enabled) {}
peahde65ddc2016-09-16 15:02:15 -0700384 // Only the rate and samples fields of capture_processing_format_ are used
385 // because the forward processing number of channels is mutable and is
386 // tracked by the capture_audio_.
387 StreamConfig capture_processing_format;
peahdf3efa82015-11-28 12:35:15 -0800388 int split_rate;
389 int stream_delay_ms;
aluebsb2328d12016-01-11 20:32:29 -0800390 bool beamformer_enabled;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700391 bool intelligibility_enabled;
peah88ac8532016-09-12 16:47:25 -0700392 bool level_controller_enabled = false;
peahe0eae3c2016-12-14 01:16:23 -0800393 bool echo_canceller3_enabled = false;
alessiob3ec96df2017-05-22 06:57:06 -0700394 bool gain_controller2_enabled = false;
peahdf3efa82015-11-28 12:35:15 -0800395 } capture_nonlocked_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000396
peahdf3efa82015-11-28 12:35:15 -0800397 struct ApmRenderState {
kwiberg83ffe452016-08-29 14:46:07 -0700398 ApmRenderState();
399 ~ApmRenderState();
kwiberg88788ad2016-02-19 07:04:49 -0800400 std::unique_ptr<AudioConverter> render_converter;
401 std::unique_ptr<AudioBuffer> render_audio;
peahdf3efa82015-11-28 12:35:15 -0800402 } render_ GUARDED_BY(crit_render_);
peah764e3642016-10-22 05:04:30 -0700403
peah701d6282016-10-25 05:42:20 -0700404 size_t aec_render_queue_element_max_size_ GUARDED_BY(crit_render_)
peah764e3642016-10-22 05:04:30 -0700405 GUARDED_BY(crit_capture_) = 0;
peah701d6282016-10-25 05:42:20 -0700406 std::vector<float> aec_render_queue_buffer_ GUARDED_BY(crit_render_);
407 std::vector<float> aec_capture_queue_buffer_ GUARDED_BY(crit_capture_);
peaha0624602016-10-25 04:45:24 -0700408
peah701d6282016-10-25 05:42:20 -0700409 size_t aecm_render_queue_element_max_size_ GUARDED_BY(crit_render_)
peaha0624602016-10-25 04:45:24 -0700410 GUARDED_BY(crit_capture_) = 0;
peah701d6282016-10-25 05:42:20 -0700411 std::vector<int16_t> aecm_render_queue_buffer_ GUARDED_BY(crit_render_);
412 std::vector<int16_t> aecm_capture_queue_buffer_ GUARDED_BY(crit_capture_);
413
414 size_t agc_render_queue_element_max_size_ GUARDED_BY(crit_render_)
415 GUARDED_BY(crit_capture_) = 0;
416 std::vector<int16_t> agc_render_queue_buffer_ GUARDED_BY(crit_render_);
417 std::vector<int16_t> agc_capture_queue_buffer_ GUARDED_BY(crit_capture_);
peah764e3642016-10-22 05:04:30 -0700418
ivoc9f4a4a02016-10-28 05:39:16 -0700419 size_t red_render_queue_element_max_size_ GUARDED_BY(crit_render_)
420 GUARDED_BY(crit_capture_) = 0;
421 std::vector<float> red_render_queue_buffer_ GUARDED_BY(crit_render_);
422 std::vector<float> red_capture_queue_buffer_ GUARDED_BY(crit_capture_);
423
peah1b08dc32016-12-20 13:45:58 -0800424 RmsLevel capture_input_rms_ GUARDED_BY(crit_capture_);
425 RmsLevel capture_output_rms_ GUARDED_BY(crit_capture_);
426 int capture_rms_interval_counter_ GUARDED_BY(crit_capture_) = 0;
henrik.lundin290d43a2016-11-29 08:09:09 -0800427
peah764e3642016-10-22 05:04:30 -0700428 // Lock protection not needed.
429 std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
peah701d6282016-10-25 05:42:20 -0700430 aec_render_signal_queue_;
peaha0624602016-10-25 04:45:24 -0700431 std::unique_ptr<
432 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
peah701d6282016-10-25 05:42:20 -0700433 aecm_render_signal_queue_;
434 std::unique_ptr<
435 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
436 agc_render_signal_queue_;
ivoc9f4a4a02016-10-28 05:39:16 -0700437 std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
438 red_render_signal_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440
niklase@google.com470e71d2011-07-07 08:21:25 +0000441} // namespace webrtc
442
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000443#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_