blob: 1ca5a8c19e7da052827b08c7916f4c38ac5405f8 [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>
ajm@google.com808e0e02011-08-03 21:08:51 +000015#include <string>
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"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000019#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020#include "webrtc/base/thread_annotations.h"
peahdf3efa82015-11-28 12:35:15 -080021#include "webrtc/modules/audio_processing/audio_buffer.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070022#include "webrtc/modules/audio_processing/include/audio_processing.h"
peahdf3efa82015-11-28 12:35:15 -080023#include "webrtc/system_wrappers/include/file_wrapper.h"
24
25#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
26// Files generated at build-time by the protobuf compiler.
27#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
28#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
29#else
30#include "webrtc/audio_processing/debug.pb.h"
31#endif
32#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34namespace webrtc {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000035
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036class AgcManagerDirect;
ekmeyerson60d9b332015-08-14 10:35:55 -070037class AudioConverter;
Michael Graczykdfa36052015-03-25 16:37:27 -070038
39template<typename T>
40class Beamformer;
41
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000042class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000043class EchoControlMobileImpl;
44class GainControlImpl;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045class GainControlForNewAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +000046class HighPassFilterImpl;
47class LevelEstimatorImpl;
48class NoiseSuppressionImpl;
49class ProcessingComponent;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000050class TransientSuppressor;
niklase@google.com470e71d2011-07-07 08:21:25 +000051class VoiceDetectionImpl;
ekmeyerson60d9b332015-08-14 10:35:55 -070052class IntelligibilityEnhancer;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
54class AudioProcessingImpl : public AudioProcessing {
55 public:
peahdf3efa82015-11-28 12:35:15 -080056 // Methods forcing APM to run in a single-threaded manner.
57 // Acquires both the render and capture locks.
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000058 explicit AudioProcessingImpl(const Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070059 // AudioProcessingImpl takes ownership of beamformer.
60 AudioProcessingImpl(const Config& config, Beamformer<float>* beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +000061 virtual ~AudioProcessingImpl();
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 int Initialize() override;
63 int Initialize(int input_sample_rate_hz,
64 int output_sample_rate_hz,
65 int reverse_sample_rate_hz,
66 ChannelLayout input_layout,
67 ChannelLayout output_layout,
68 ChannelLayout reverse_layout) override;
Michael Graczyk86c6d332015-07-23 11:41:39 -070069 int Initialize(const ProcessingConfig& processing_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000070 void SetExtraOptions(const Config& config) override;
peahdf3efa82015-11-28 12:35:15 -080071 void UpdateHistogramsOnCallEnd() override;
72 int StartDebugRecording(const char filename[kMaxFilenameSize]) override;
73 int StartDebugRecording(FILE* handle) override;
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.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.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 int AnalyzeReverseStream(AudioFrame* frame) override;
ekmeyerson60d9b332015-08-14 10:35:55 -0700100 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700102 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000103 int sample_rate_hz,
104 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -0700105 int ProcessReverseStream(const float* const* src,
106 const StreamConfig& reverse_input_config,
107 const StreamConfig& reverse_output_config,
108 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -0800109
110 // Methods only accessed from APM submodules or
111 // from AudioProcessing tests in a single-threaded manner.
112 // Hence there is no need for locks in these.
113 int proc_sample_rate_hz() const override;
114 int proc_split_sample_rate_hz() const override;
115 int num_input_channels() const override;
116 int num_output_channels() const override;
117 int 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
122 // Methods returning pointers to APM submodules.
123 // No locks are aquired in those, as those locks
124 // would offer no protection (the submodules are
125 // created only once in a single-treaded manner
126 // during APM creation).
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000127 EchoCancellation* echo_cancellation() const override;
128 EchoControlMobile* echo_control_mobile() const override;
129 GainControl* gain_control() const override;
130 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
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000135 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000136 // Overridden in a mock.
peahdf3efa82015-11-28 12:35:15 -0800137 virtual int InitializeLocked()
138 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000139
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 private:
peahdf3efa82015-11-28 12:35:15 -0800141 struct ApmPublicSubmodules;
142 struct ApmPrivateSubmodules;
143
144#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
145 // State for the debug dump.
146 struct ApmDebugDumpThreadState {
147 ApmDebugDumpThreadState() : event_msg(new audioproc::Event()) {}
148 rtc::scoped_ptr<audioproc::Event> event_msg; // Protobuf message.
149 std::string event_str; // Memory for protobuf serialization.
150
151 // Serialized string of last saved APM configuration.
152 std::string last_serialized_config;
153 };
154
155 struct ApmDebugDumpState {
156 ApmDebugDumpState() : debug_file(FileWrapper::Create()) {}
157 rtc::scoped_ptr<FileWrapper> debug_file;
158 ApmDebugDumpThreadState render;
159 ApmDebugDumpThreadState capture;
160 };
161#endif
162
163 // Method for modifying the formats struct that are called from both
164 // the render and capture threads. The check for whether modifications
165 // are needed is done while holding the render lock only, thereby avoiding
166 // that the capture thread blocks the render thread.
167 // The struct is modified in a single-threaded manner by holding both the
168 // render and capture locks.
169 int MaybeInitialize(const ProcessingConfig& config)
170 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
171
172 int MaybeInitializeRender(const ProcessingConfig& processing_config)
173 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
174
175 int MaybeInitializeCapture(const ProcessingConfig& processing_config)
176 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
177
178 // Method for checking for the need of conversion. Accesses the formats
179 // structs in a read manner but the requirement for the render lock to be held
180 // was added as it currently anyway is always called in that manner.
181 bool rev_conversion_needed() const EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
182 bool render_check_rev_conversion_needed() const
183 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
184
185 // Methods requiring APM running in a single-threaded manner.
186 // Are called with both the render and capture locks already
187 // acquired.
188 void InitializeExperimentalAgc()
189 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
190 void InitializeTransient()
191 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
192 void InitializeBeamformer()
193 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
194 void InitializeIntelligibility()
195 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
solenberg70f99032015-12-08 11:07:32 -0800196 void InitializeHighPassFilter()
197 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700198 int InitializeLocked(const ProcessingConfig& config)
peahdf3efa82015-11-28 12:35:15 -0800199 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
200
201 // Capture-side exclusive methods possibly running APM in a multi-threaded
202 // manner that are called with the render lock already acquired.
203 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
204 bool output_copy_needed(bool is_data_processed) const
205 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
206 bool is_data_processed() const EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
207 bool synthesis_needed(bool is_data_processed) const
208 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
209 bool analysis_needed(bool is_data_processed) const
210 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
211 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
212
213 // Render-side exclusive methods possibly running APM in a multi-threaded
214 // manner that are called with the render lock already acquired.
ekmeyerson60d9b332015-08-14 10:35:55 -0700215 // TODO(ekm): Remove once all clients updated to new interface.
peahdf3efa82015-11-28 12:35:15 -0800216 int AnalyzeReverseStreamLocked(const float* const* src,
217 const StreamConfig& input_config,
218 const StreamConfig& output_config)
219 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
220 bool is_rev_processed() const EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
221 int ProcessReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000222
peahdf3efa82015-11-28 12:35:15 -0800223// Debug dump methods that are internal and called without locks.
224// TODO(peah): Make thread safe.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000225#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
226 // TODO(andrew): make this more graceful. Ideally we would split this stuff
227 // out into a separate class with an "enabled" and "disabled" implementation.
peahdf3efa82015-11-28 12:35:15 -0800228 static int WriteMessageToDebugFile(FileWrapper* debug_file,
229 rtc::CriticalSection* crit_debug,
230 ApmDebugDumpThreadState* debug_state);
231 int WriteInitMessage() EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200232
233 // Writes Config message. If not |forced|, only writes the current config if
234 // it is different from the last saved one; if |forced|, writes the config
235 // regardless of the last saved.
peahdf3efa82015-11-28 12:35:15 -0800236 int WriteConfigMessage(bool forced) EXCLUSIVE_LOCKS_REQUIRED(crit_capture_)
237 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200238
peahdf3efa82015-11-28 12:35:15 -0800239 // Critical section.
240 mutable rtc::CriticalSection crit_debug_;
Minyue13b96ba2015-10-03 00:39:14 +0200241
peahdf3efa82015-11-28 12:35:15 -0800242 // Debug dump state.
243 ApmDebugDumpState debug_dump_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000244#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
peahdf3efa82015-11-28 12:35:15 -0800246 // Critical sections.
247 mutable rtc::CriticalSection crit_render_ ACQUIRED_BEFORE(crit_capture_);
248 mutable rtc::CriticalSection crit_capture_;
249
250 // Structs containing the pointers to the submodules.
251 rtc::scoped_ptr<ApmPublicSubmodules> public_submodules_;
252 rtc::scoped_ptr<ApmPrivateSubmodules> private_submodules_
253 GUARDED_BY(crit_capture_);
254
peah192164e2015-11-17 02:16:45 -0800255 // State that is written to while holding both the render and capture locks
peahdf3efa82015-11-28 12:35:15 -0800256 // but can be read without any lock being held.
257 // As this is only accessed internally of APM, and all internal methods in APM
258 // either are holding the render or capture locks, this construct is safe as
259 // it is not possible to read the variables while writing them.
260 struct ApmFormatState {
261 ApmFormatState()
peah192164e2015-11-17 02:16:45 -0800262 : // Format of processing streams at input/output call sites.
peahdf3efa82015-11-28 12:35:15 -0800263 api_format({{{kSampleRate16kHz, 1, false},
264 {kSampleRate16kHz, 1, false},
265 {kSampleRate16kHz, 1, false},
266 {kSampleRate16kHz, 1, false}}}),
267 rev_proc_format(kSampleRate16kHz, 1) {}
268 ProcessingConfig api_format;
269 StreamConfig rev_proc_format;
270 } formats_;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700271
peahdf3efa82015-11-28 12:35:15 -0800272 // APM constants.
273 const struct ApmConstants {
274 ApmConstants(int agc_startup_min_volume,
275 const std::vector<Point> array_geometry,
276 SphericalPointf target_direction,
277 bool use_new_agc,
278 bool intelligibility_enabled,
279 bool beamformer_enabled)
280 : // Format of processing streams at input/output call sites.
281 agc_startup_min_volume(agc_startup_min_volume),
282 array_geometry(array_geometry),
283 target_direction(target_direction),
284 use_new_agc(use_new_agc),
285 intelligibility_enabled(intelligibility_enabled),
286 beamformer_enabled(beamformer_enabled) {}
287 int agc_startup_min_volume;
288 std::vector<Point> array_geometry;
289 SphericalPointf target_direction;
290 bool use_new_agc;
291 bool intelligibility_enabled;
292 bool beamformer_enabled;
293 } constants_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000294
peahdf3efa82015-11-28 12:35:15 -0800295 struct ApmCaptureState {
296 ApmCaptureState(bool transient_suppressor_enabled)
297 : aec_system_delay_jumps(-1),
298 delay_offset_ms(0),
299 was_stream_delay_set(false),
300 last_stream_delay_ms(0),
301 last_aec_system_delay_ms(0),
302 stream_delay_jumps(-1),
303 output_will_be_muted(false),
304 key_pressed(false),
305 transient_suppressor_enabled(transient_suppressor_enabled),
306 fwd_proc_format(kSampleRate16kHz),
307 split_rate(kSampleRate16kHz) {}
308 int aec_system_delay_jumps;
309 int delay_offset_ms;
310 bool was_stream_delay_set;
311 int last_stream_delay_ms;
312 int last_aec_system_delay_ms;
313 int stream_delay_jumps;
314 bool output_will_be_muted;
315 bool key_pressed;
316 bool transient_suppressor_enabled;
317 rtc::scoped_ptr<AudioBuffer> capture_audio;
318 // Only the rate and samples fields of fwd_proc_format_ are used because the
319 // forward processing number of channels is mutable and is tracked by the
320 // capture_audio_.
321 StreamConfig fwd_proc_format;
322 int split_rate;
323 } capture_ GUARDED_BY(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
peahdf3efa82015-11-28 12:35:15 -0800325 struct ApmCaptureNonLockedState {
326 ApmCaptureNonLockedState()
327 : fwd_proc_format(kSampleRate16kHz),
328 split_rate(kSampleRate16kHz),
329 stream_delay_ms(0) {}
330 // Only the rate and samples fields of fwd_proc_format_ are used because the
331 // forward processing number of channels is mutable and is tracked by the
332 // capture_audio_.
333 StreamConfig fwd_proc_format;
334 int split_rate;
335 int stream_delay_ms;
336 } capture_nonlocked_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000337
peahdf3efa82015-11-28 12:35:15 -0800338 struct ApmRenderState {
339 rtc::scoped_ptr<AudioConverter> render_converter;
340 rtc::scoped_ptr<AudioBuffer> render_audio;
341 } render_ GUARDED_BY(crit_render_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000342};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000343
niklase@google.com470e71d2011-07-07 08:21:25 +0000344} // namespace webrtc
345
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000346#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_