blob: 4f70bd66b2b5de28350dc6f596ec846f62193652 [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
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020018#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"
peahdf3efa82015-11-28 12:35:15 -080025#include "webrtc/modules/audio_processing/audio_buffer.h"
aleloi868f32f2017-05-23 07:20:05 -070026#include "webrtc/modules/audio_processing/include/aec_dump.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070027#include "webrtc/modules/audio_processing/include/audio_processing.h"
peah764e3642016-10-22 05:04:30 -070028#include "webrtc/modules/audio_processing/render_queue_item_verifier.h"
henrik.lundin290d43a2016-11-29 08:09:09 -080029#include "webrtc/modules/audio_processing/rms_level.h"
peahdf3efa82015-11-28 12:35:15 -080030#include "webrtc/system_wrappers/include/file_wrapper.h"
31
32#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
mbonadei7c2c8432017-04-07 00:59:12 -070033// *.pb.h files are generated at build-time by the protobuf compiler.
kwiberg77eab702016-09-28 17:42:01 -070034RTC_PUSH_IGNORING_WUNDEF()
peahdf3efa82015-11-28 12:35:15 -080035#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
36#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
37#else
kjellander78ddd732016-02-09 08:13:06 -080038#include "webrtc/modules/audio_processing/debug.pb.h"
peahdf3efa82015-11-28 12:35:15 -080039#endif
kwiberg77eab702016-09-28 17:42:01 -070040RTC_POP_IGNORING_WUNDEF()
peahdf3efa82015-11-28 12:35:15 -080041#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000042
43namespace webrtc {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000044
ekmeyerson60d9b332015-08-14 10:35:55 -070045class AudioConverter;
Alejandro Luebsf4022ff2016-07-01 17:19:09 -070046class NonlinearBeamformer;
Michael Graczykdfa36052015-03-25 16:37:27 -070047
niklase@google.com470e71d2011-07-07 08:21:25 +000048class AudioProcessingImpl : public AudioProcessing {
49 public:
peahdf3efa82015-11-28 12:35:15 -080050 // Methods forcing APM to run in a single-threaded manner.
51 // Acquires both the render and capture locks.
peah88ac8532016-09-12 16:47:25 -070052 explicit AudioProcessingImpl(const webrtc::Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070053 // AudioProcessingImpl takes ownership of beamformer.
peah88ac8532016-09-12 16:47:25 -070054 AudioProcessingImpl(const webrtc::Config& config,
55 NonlinearBeamformer* beamformer);
kwiberg83ffe452016-08-29 14:46:07 -070056 ~AudioProcessingImpl() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000057 int Initialize() override;
peahde65ddc2016-09-16 15:02:15 -070058 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 Graczyk86c6d332015-07-23 11:41:39 -070064 int Initialize(const ProcessingConfig& processing_config) override;
peah88ac8532016-09-12 16:47:25 -070065 void ApplyConfig(const AudioProcessing::Config& config) override;
66 void SetExtraOptions(const webrtc::Config& config) override;
peahdf3efa82015-11-28 12:35:15 -080067 void UpdateHistogramsOnCallEnd() override;
aleloi868f32f2017-05-23 07:20:05 -070068 void AttachAecDump(std::unique_ptr<AecDump> aec_dump) override;
69 void DetachAecDump() override;
ivocd66b44d2016-01-15 03:06:36 -080070 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;
peah73a28ee2016-10-12 03:01:49 -070073 int StartDebugRecording(FILE* handle) override;
peahdf3efa82015-11-28 12:35:15 -080074 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.org14665ff2015-03-04 12:58:35 +000079 int ProcessStream(AudioFrame* frame) override;
80 int ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -070081 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 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 Graczyk86c6d332015-07-23 11:41:39 -070087 int ProcessStream(const float* const* src,
88 const StreamConfig& input_config,
89 const StreamConfig& output_config,
90 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -080091 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.
ekmeyerson60d9b332015-08-14 10:35:55 -070099 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 int AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700101 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 int sample_rate_hz,
103 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -0700104 int ProcessReverseStream(const float* const* src,
peahde65ddc2016-09-16 15:02:15 -0700105 const StreamConfig& input_config,
106 const StreamConfig& output_config,
ekmeyerson60d9b332015-08-14 10:35:55 -0700107 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -0800108
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 Kasting69558702016-01-12 16:26:35 -0800114 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.org14665ff2015-03-04 12:58:35 +0000118 int stream_delay_ms() const override;
peahdf3efa82015-11-28 12:35:15 -0800119 bool was_stream_delay_set() const override
120 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
121
ivoc3e9a5372016-10-28 07:55:33 -0700122 AudioProcessingStatistics GetStatistics() const override;
123
peahdf3efa82015-11-28 12:35:15 -0800124 // 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.org14665ff2015-03-04 12:58:35 +0000129 EchoCancellation* echo_cancellation() const override;
130 EchoControlMobile* echo_control_mobile() const override;
131 GainControl* gain_control() const override;
peah8271d042016-11-22 07:24:52 -0800132 // TODO(peah): Deprecate this API call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000133 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.com470e71d2011-07-07 08:21:25 +0000137
henrik.lundinadf06352017-04-05 05:48:24 -0700138 // TODO(peah): Remove MutateConfig once the new API allows that.
peah8271d042016-11-22 07:24:52 -0800139 void MutateConfig(rtc::FunctionView<void(AudioProcessing::Config*)> mutator);
henrik.lundinadf06352017-04-05 05:48:24 -0700140 AudioProcessing::Config GetConfig() const override;
peah8271d042016-11-22 07:24:52 -0800141
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000142 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000143 // Overridden in a mock.
peahdf3efa82015-11-28 12:35:15 -0800144 virtual int InitializeLocked()
145 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000146
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 private:
peahc19f3122016-10-07 14:54:10 -0700148 // 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);
peahdf3efa82015-11-28 12:35:15 -0800153 struct ApmPublicSubmodules;
154 struct ApmPrivateSubmodules;
155
peah8271d042016-11-22 07:24:52 -0800156 // Submodule interface implementations.
157 std::unique_ptr<HighPassFilter> high_pass_filter_impl_;
158
peah2ace3f92016-09-10 04:42:27 -0700159 class ApmSubmoduleStates {
160 public:
161 ApmSubmoduleStates();
162 // Updates the submodule state and returns true if it has changed.
peah8271d042016-11-22 07:24:52 -0800163 bool Update(bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700164 bool echo_canceller_enabled,
165 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700166 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700167 bool noise_suppressor_enabled,
168 bool intelligibility_enhancer_enabled,
169 bool beamformer_enabled,
170 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700171 bool gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -0700172 bool level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -0800173 bool echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -0700174 bool voice_activity_detector_enabled,
175 bool level_estimator_enabled,
176 bool transient_suppressor_enabled);
177 bool CaptureMultiBandSubModulesActive() const;
178 bool CaptureMultiBandProcessingActive() const;
peah23ac8b42017-05-23 05:33:56 -0700179 bool CaptureFullBandProcessingActive() const;
peah2ace3f92016-09-10 04:42:27 -0700180 bool RenderMultiBandSubModulesActive() const;
181 bool RenderMultiBandProcessingActive() const;
182
183 private:
peah8271d042016-11-22 07:24:52 -0800184 bool low_cut_filter_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700185 bool echo_canceller_enabled_ = false;
186 bool mobile_echo_controller_enabled_ = false;
ivoc9f4a4a02016-10-28 05:39:16 -0700187 bool residual_echo_detector_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700188 bool noise_suppressor_enabled_ = false;
189 bool intelligibility_enhancer_enabled_ = false;
190 bool beamformer_enabled_ = false;
191 bool adaptive_gain_controller_enabled_ = false;
alessiob3ec96df2017-05-22 06:57:06 -0700192 bool gain_controller2_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700193 bool level_controller_enabled_ = false;
peahe0eae3c2016-12-14 01:16:23 -0800194 bool echo_canceller3_enabled_ = false;
peah2ace3f92016-09-10 04:42:27 -0700195 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
peahdf3efa82015-11-28 12:35:15 -0800201#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
202 // State for the debug dump.
203 struct ApmDebugDumpThreadState {
kwiberg83ffe452016-08-29 14:46:07 -0700204 ApmDebugDumpThreadState();
205 ~ApmDebugDumpThreadState();
kwiberg88788ad2016-02-19 07:04:49 -0800206 std::unique_ptr<audioproc::Event> event_msg; // Protobuf message.
mbonadei7c2c8432017-04-07 00:59:12 -0700207 ProtoString event_str; // Memory for protobuf serialization.
peahdf3efa82015-11-28 12:35:15 -0800208
209 // Serialized string of last saved APM configuration.
mbonadei7c2c8432017-04-07 00:59:12 -0700210 ProtoString last_serialized_config;
peahdf3efa82015-11-28 12:35:15 -0800211 };
212
213 struct ApmDebugDumpState {
kwiberg83ffe452016-08-29 14:46:07 -0700214 ApmDebugDumpState();
215 ~ApmDebugDumpState();
ivocd66b44d2016-01-15 03:06:36 -0800216 // 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;
kwiberg88788ad2016-02-19 07:04:49 -0800219 std::unique_ptr<FileWrapper> debug_file;
peahdf3efa82015-11-28 12:35:15 -0800220 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.
peah2ace3f92016-09-10 04:42:27 -0700231 int MaybeInitialize(const ProcessingConfig& config, bool force_initialization)
peahdf3efa82015-11-28 12:35:15 -0800232 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
233
234 int MaybeInitializeRender(const ProcessingConfig& processing_config)
235 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
236
peah2ace3f92016-09-10 04:42:27 -0700237 int MaybeInitializeCapture(const ProcessingConfig& processing_config,
238 bool force_initialization)
peahdf3efa82015-11-28 12:35:15 -0800239 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
240
peah2ace3f92016-09-10 04:42:27 -0700241 // 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_);
peahdf3efa82015-11-28 12:35:15 -0800244
245 // Methods requiring APM running in a single-threaded manner.
246 // Are called with both the render and capture locks already
247 // acquired.
peahdf3efa82015-11-28 12:35:15 -0800248 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 Graczyk86c6d332015-07-23 11:41:39 -0700254 int InitializeLocked(const ProcessingConfig& config)
peahdf3efa82015-11-28 12:35:15 -0800255 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peahca4cac72016-06-29 15:26:12 -0700256 void InitializeLevelController() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
ivoc9f4a4a02016-10-28 05:39:16 -0700257 void InitializeResidualEchoDetector()
258 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peah8271d042016-11-22 07:24:52 -0800259 void InitializeLowCutFilter() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahe0eae3c2016-12-14 01:16:23 -0800260 void InitializeEchoCanceller3() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
alessiob3ec96df2017-05-22 06:57:06 -0700261 void InitializeGainController2();
peahdf3efa82015-11-28 12:35:15 -0800262
peah764e3642016-10-22 05:04:30 -0700263 void EmptyQueuedRenderAudio();
264 void AllocateRenderQueue()
265 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
peah9e6a2902017-05-15 07:19:21 -0700266 void QueueBandedRenderAudio(AudioBuffer* audio)
267 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
268 void QueueNonbandedRenderAudio(AudioBuffer* audio)
peah764e3642016-10-22 05:04:30 -0700269 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
270
peahdf3efa82015-11-28 12:35:15 -0800271 // Capture-side exclusive methods possibly running APM in a multi-threaded
272 // manner that are called with the render lock already acquired.
peahde65ddc2016-09-16 15:02:15 -0700273 int ProcessCaptureStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800274 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.
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 // TODO(ekm): Remove once all clients updated to new interface.
peahdf3efa82015-11-28 12:35:15 -0800279 int AnalyzeReverseStreamLocked(const float* const* src,
280 const StreamConfig& input_config,
281 const StreamConfig& output_config)
282 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
peahde65ddc2016-09-16 15:02:15 -0700283 int ProcessRenderStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000284
aleloi868f32f2017-05-23 07:20:05 -0700285 // 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
peahdf3efa82015-11-28 12:35:15 -0800313// Debug dump methods that are internal and called without locks.
314// TODO(peah): Make thread safe.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000315#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.
peahdf3efa82015-11-28 12:35:15 -0800318 static int WriteMessageToDebugFile(FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -0800319 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -0800320 rtc::CriticalSection* crit_debug,
321 ApmDebugDumpThreadState* debug_state);
322 int WriteInitMessage() EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200323
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.
peahdf3efa82015-11-28 12:35:15 -0800327 int WriteConfigMessage(bool forced) EXCLUSIVE_LOCKS_REQUIRED(crit_capture_)
328 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200329
peahdf3efa82015-11-28 12:35:15 -0800330 // Critical section.
pbos5ad935c2016-01-25 03:52:44 -0800331 rtc::CriticalSection crit_debug_;
Minyue13b96ba2015-10-03 00:39:14 +0200332
peahdf3efa82015-11-28 12:35:15 -0800333 // Debug dump state.
334 ApmDebugDumpState debug_dump_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000335#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000336
aleloi868f32f2017-05-23 07:20:05 -0700337 // 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
peahdf3efa82015-11-28 12:35:15 -0800345 // Critical sections.
pbos5ad935c2016-01-25 03:52:44 -0800346 rtc::CriticalSection crit_render_ ACQUIRED_BEFORE(crit_capture_);
347 rtc::CriticalSection crit_capture_;
peahdf3efa82015-11-28 12:35:15 -0800348
peahc19f3122016-10-07 14:54:10 -0700349 // Struct containing the Config specifying the behavior of APM.
350 AudioProcessing::Config config_;
351
peah2ace3f92016-09-10 04:42:27 -0700352 // Class containing information about what submodules are active.
353 ApmSubmoduleStates submodule_states_;
354
peahdf3efa82015-11-28 12:35:15 -0800355 // Structs containing the pointers to the submodules.
kwiberg88788ad2016-02-19 07:04:49 -0800356 std::unique_ptr<ApmPublicSubmodules> public_submodules_;
ivoc9f4a4a02016-10-28 05:39:16 -0700357 std::unique_ptr<ApmPrivateSubmodules> private_submodules_;
peahdf3efa82015-11-28 12:35:15 -0800358
peah192164e2015-11-17 02:16:45 -0800359 // State that is written to while holding both the render and capture locks
peahdf3efa82015-11-28 12:35:15 -0800360 // 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()
peah192164e2015-11-17 02:16:45 -0800366 : // Format of processing streams at input/output call sites.
peahdf3efa82015-11-28 12:35:15 -0800367 api_format({{{kSampleRate16kHz, 1, false},
368 {kSampleRate16kHz, 1, false},
369 {kSampleRate16kHz, 1, false},
370 {kSampleRate16kHz, 1, false}}}),
peahde65ddc2016-09-16 15:02:15 -0700371 render_processing_format(kSampleRate16kHz, 1) {}
peahdf3efa82015-11-28 12:35:15 -0800372 ProcessingConfig api_format;
peahde65ddc2016-09-16 15:02:15 -0700373 StreamConfig render_processing_format;
peahdf3efa82015-11-28 12:35:15 -0800374 } formats_;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700375
peahdf3efa82015-11-28 12:35:15 -0800376 // APM constants.
377 const struct ApmConstants {
henrik.lundinbd681b92016-12-05 09:08:42 -0800378 ApmConstants(int agc_startup_min_volume,
379 int agc_clipped_level_min,
380 bool use_experimental_agc)
peahdf3efa82015-11-28 12:35:15 -0800381 : // Format of processing streams at input/output call sites.
382 agc_startup_min_volume(agc_startup_min_volume),
henrik.lundinbd681b92016-12-05 09:08:42 -0800383 agc_clipped_level_min(agc_clipped_level_min),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700384 use_experimental_agc(use_experimental_agc) {}
peahdf3efa82015-11-28 12:35:15 -0800385 int agc_startup_min_volume;
henrik.lundinbd681b92016-12-05 09:08:42 -0800386 int agc_clipped_level_min;
peahbe615622016-02-13 16:40:47 -0800387 bool use_experimental_agc;
peahdf3efa82015-11-28 12:35:15 -0800388 } constants_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000389
peahdf3efa82015-11-28 12:35:15 -0800390 struct ApmCaptureState {
aluebs2a346882016-01-11 18:04:30 -0800391 ApmCaptureState(bool transient_suppressor_enabled,
aluebs2a346882016-01-11 18:04:30 -0800392 const std::vector<Point>& array_geometry,
kwiberg83ffe452016-08-29 14:46:07 -0700393 SphericalPointf target_direction);
394 ~ApmCaptureState();
peahdf3efa82015-11-28 12:35:15 -0800395 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;
aluebs2a346882016-01-11 18:04:30 -0800404 std::vector<Point> array_geometry;
405 SphericalPointf target_direction;
kwiberg88788ad2016-02-19 07:04:49 -0800406 std::unique_ptr<AudioBuffer> capture_audio;
peahde65ddc2016-09-16 15:02:15 -0700407 // 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;
peahdf3efa82015-11-28 12:35:15 -0800411 int split_rate;
peah67995532017-04-10 14:12:41 -0700412 bool echo_path_gain_change;
peahdf3efa82015-11-28 12:35:15 -0800413 } capture_ GUARDED_BY(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
peahdf3efa82015-11-28 12:35:15 -0800415 struct ApmCaptureNonLockedState {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700416 ApmCaptureNonLockedState(bool beamformer_enabled,
peah88ac8532016-09-12 16:47:25 -0700417 bool intelligibility_enabled)
peahde65ddc2016-09-16 15:02:15 -0700418 : capture_processing_format(kSampleRate16kHz),
peahdf3efa82015-11-28 12:35:15 -0800419 split_rate(kSampleRate16kHz),
aluebsb2328d12016-01-11 20:32:29 -0800420 stream_delay_ms(0),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700421 beamformer_enabled(beamformer_enabled),
peah88ac8532016-09-12 16:47:25 -0700422 intelligibility_enabled(intelligibility_enabled) {}
peahde65ddc2016-09-16 15:02:15 -0700423 // 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;
peahdf3efa82015-11-28 12:35:15 -0800427 int split_rate;
428 int stream_delay_ms;
aluebsb2328d12016-01-11 20:32:29 -0800429 bool beamformer_enabled;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700430 bool intelligibility_enabled;
peah88ac8532016-09-12 16:47:25 -0700431 bool level_controller_enabled = false;
peahe0eae3c2016-12-14 01:16:23 -0800432 bool echo_canceller3_enabled = false;
alessiob3ec96df2017-05-22 06:57:06 -0700433 bool gain_controller2_enabled = false;
peahdf3efa82015-11-28 12:35:15 -0800434 } capture_nonlocked_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000435
peahdf3efa82015-11-28 12:35:15 -0800436 struct ApmRenderState {
kwiberg83ffe452016-08-29 14:46:07 -0700437 ApmRenderState();
438 ~ApmRenderState();
kwiberg88788ad2016-02-19 07:04:49 -0800439 std::unique_ptr<AudioConverter> render_converter;
440 std::unique_ptr<AudioBuffer> render_audio;
peahdf3efa82015-11-28 12:35:15 -0800441 } render_ GUARDED_BY(crit_render_);
peah764e3642016-10-22 05:04:30 -0700442
peah701d6282016-10-25 05:42:20 -0700443 size_t aec_render_queue_element_max_size_ GUARDED_BY(crit_render_)
peah764e3642016-10-22 05:04:30 -0700444 GUARDED_BY(crit_capture_) = 0;
peah701d6282016-10-25 05:42:20 -0700445 std::vector<float> aec_render_queue_buffer_ GUARDED_BY(crit_render_);
446 std::vector<float> aec_capture_queue_buffer_ GUARDED_BY(crit_capture_);
peaha0624602016-10-25 04:45:24 -0700447
peah701d6282016-10-25 05:42:20 -0700448 size_t aecm_render_queue_element_max_size_ GUARDED_BY(crit_render_)
peaha0624602016-10-25 04:45:24 -0700449 GUARDED_BY(crit_capture_) = 0;
peah701d6282016-10-25 05:42:20 -0700450 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_);
peah764e3642016-10-22 05:04:30 -0700457
ivoc9f4a4a02016-10-28 05:39:16 -0700458 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
peah1b08dc32016-12-20 13:45:58 -0800463 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.lundin290d43a2016-11-29 08:09:09 -0800466
peah764e3642016-10-22 05:04:30 -0700467 // Lock protection not needed.
468 std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
peah701d6282016-10-25 05:42:20 -0700469 aec_render_signal_queue_;
peaha0624602016-10-25 04:45:24 -0700470 std::unique_ptr<
471 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
peah701d6282016-10-25 05:42:20 -0700472 aecm_render_signal_queue_;
473 std::unique_ptr<
474 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
475 agc_render_signal_queue_;
ivoc9f4a4a02016-10-28 05:39:16 -0700476 std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
477 red_render_signal_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000479
niklase@google.com470e71d2011-07-07 08:21:25 +0000480} // namespace webrtc
481
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000482#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_