blob: 560920b1e19a63006b23645edb029d5542c96743 [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>
ajm@google.com808e0e02011-08-03 21:08:51 +000016#include <string>
Michael Graczyk86c6d332015-07-23 11:41:39 -070017#include <vector>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
peahdf3efa82015-11-28 12:35:15 -080019#include "webrtc/base/criticalsection.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
kjellander78ddd732016-02-09 08:13:06 -080030#include "webrtc/modules/audio_processing/debug.pb.h"
peahdf3efa82015-11-28 12:35:15 -080031#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
niklase@google.com470e71d2011-07-07 08:21:25 +000042class AudioProcessingImpl : public AudioProcessing {
43 public:
peahdf3efa82015-11-28 12:35:15 -080044 // Methods forcing APM to run in a single-threaded manner.
45 // Acquires both the render and capture locks.
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000046 explicit AudioProcessingImpl(const Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070047 // AudioProcessingImpl takes ownership of beamformer.
48 AudioProcessingImpl(const Config& config, Beamformer<float>* beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +000049 virtual ~AudioProcessingImpl();
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000050 int Initialize() override;
51 int Initialize(int input_sample_rate_hz,
52 int output_sample_rate_hz,
53 int reverse_sample_rate_hz,
54 ChannelLayout input_layout,
55 ChannelLayout output_layout,
56 ChannelLayout reverse_layout) override;
Michael Graczyk86c6d332015-07-23 11:41:39 -070057 int Initialize(const ProcessingConfig& processing_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 void SetExtraOptions(const Config& config) override;
peahdf3efa82015-11-28 12:35:15 -080059 void UpdateHistogramsOnCallEnd() override;
ivocd66b44d2016-01-15 03:06:36 -080060 int StartDebugRecording(const char filename[kMaxFilenameSize],
61 int64_t max_log_size_bytes) override;
62 int StartDebugRecording(FILE* handle, int64_t max_log_size_bytes) override;
63
peahdf3efa82015-11-28 12:35:15 -080064 int StartDebugRecordingForPlatformFile(rtc::PlatformFile handle) override;
65 int StopDebugRecording() override;
66
67 // Capture-side exclusive methods possibly running APM in a
68 // multi-threaded manner. Acquire the capture lock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000069 int ProcessStream(AudioFrame* frame) override;
70 int ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 int input_sample_rate_hz,
73 ChannelLayout input_layout,
74 int output_sample_rate_hz,
75 ChannelLayout output_layout,
76 float* const* dest) override;
Michael Graczyk86c6d332015-07-23 11:41:39 -070077 int ProcessStream(const float* const* src,
78 const StreamConfig& input_config,
79 const StreamConfig& output_config,
80 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -080081 void set_output_will_be_muted(bool muted) override;
82 int set_stream_delay_ms(int delay) override;
83 void set_delay_offset_ms(int offset) override;
84 int delay_offset_ms() const override;
85 void set_stream_key_pressed(bool key_pressed) override;
peah66085be2015-12-16 02:02:20 -080086 int input_sample_rate_hz() const override;
peahdf3efa82015-11-28 12:35:15 -080087
88 // Render-side exclusive methods possibly running APM in a
89 // multi-threaded manner. Acquire the render lock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000090 int AnalyzeReverseStream(AudioFrame* frame) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070091 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -070093 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000094 int sample_rate_hz,
95 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070096 int ProcessReverseStream(const float* const* src,
97 const StreamConfig& reverse_input_config,
98 const StreamConfig& reverse_output_config,
99 float* const* dest) override;
peahdf3efa82015-11-28 12:35:15 -0800100
101 // Methods only accessed from APM submodules or
102 // from AudioProcessing tests in a single-threaded manner.
103 // Hence there is no need for locks in these.
104 int proc_sample_rate_hz() const override;
105 int proc_split_sample_rate_hz() const override;
Peter Kasting69558702016-01-12 16:26:35 -0800106 size_t num_input_channels() const override;
107 size_t num_proc_channels() const override;
108 size_t num_output_channels() const override;
109 size_t num_reverse_channels() const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 int stream_delay_ms() const override;
peahdf3efa82015-11-28 12:35:15 -0800111 bool was_stream_delay_set() const override
112 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
113
114 // Methods returning pointers to APM submodules.
115 // No locks are aquired in those, as those locks
116 // would offer no protection (the submodules are
117 // created only once in a single-treaded manner
118 // during APM creation).
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 EchoCancellation* echo_cancellation() const override;
120 EchoControlMobile* echo_control_mobile() const override;
121 GainControl* gain_control() const override;
122 HighPassFilter* high_pass_filter() const override;
123 LevelEstimator* level_estimator() const override;
124 NoiseSuppression* noise_suppression() const override;
125 VoiceDetection* voice_detection() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000127 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000128 // Overridden in a mock.
peahdf3efa82015-11-28 12:35:15 -0800129 virtual int InitializeLocked()
130 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000131
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 private:
peahdf3efa82015-11-28 12:35:15 -0800133 struct ApmPublicSubmodules;
134 struct ApmPrivateSubmodules;
135
136#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
137 // State for the debug dump.
138 struct ApmDebugDumpThreadState {
139 ApmDebugDumpThreadState() : event_msg(new audioproc::Event()) {}
kwiberg88788ad2016-02-19 07:04:49 -0800140 std::unique_ptr<audioproc::Event> event_msg; // Protobuf message.
peahdf3efa82015-11-28 12:35:15 -0800141 std::string event_str; // Memory for protobuf serialization.
142
143 // Serialized string of last saved APM configuration.
144 std::string last_serialized_config;
145 };
146
147 struct ApmDebugDumpState {
148 ApmDebugDumpState() : debug_file(FileWrapper::Create()) {}
ivocd66b44d2016-01-15 03:06:36 -0800149 // Number of bytes that can still be written to the log before the maximum
150 // size is reached. A value of <= 0 indicates that no limit is used.
151 int64_t num_bytes_left_for_log_ = -1;
kwiberg88788ad2016-02-19 07:04:49 -0800152 std::unique_ptr<FileWrapper> debug_file;
peahdf3efa82015-11-28 12:35:15 -0800153 ApmDebugDumpThreadState render;
154 ApmDebugDumpThreadState capture;
155 };
156#endif
157
158 // Method for modifying the formats struct that are called from both
159 // the render and capture threads. The check for whether modifications
160 // are needed is done while holding the render lock only, thereby avoiding
161 // that the capture thread blocks the render thread.
162 // The struct is modified in a single-threaded manner by holding both the
163 // render and capture locks.
164 int MaybeInitialize(const ProcessingConfig& config)
165 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
166
167 int MaybeInitializeRender(const ProcessingConfig& processing_config)
168 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
169
170 int MaybeInitializeCapture(const ProcessingConfig& processing_config)
171 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
172
173 // Method for checking for the need of conversion. Accesses the formats
174 // structs in a read manner but the requirement for the render lock to be held
175 // was added as it currently anyway is always called in that manner.
176 bool rev_conversion_needed() const EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
177 bool render_check_rev_conversion_needed() const
178 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
179
180 // Methods requiring APM running in a single-threaded manner.
181 // Are called with both the render and capture locks already
182 // acquired.
183 void InitializeExperimentalAgc()
184 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
185 void InitializeTransient()
186 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
187 void InitializeBeamformer()
188 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
189 void InitializeIntelligibility()
190 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
solenberg70f99032015-12-08 11:07:32 -0800191 void InitializeHighPassFilter()
192 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
solenberg5e465c32015-12-08 13:22:33 -0800193 void InitializeNoiseSuppression()
194 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
solenberg949028f2015-12-15 11:39:38 -0800195 void InitializeLevelEstimator()
196 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
solenberga29386c2015-12-16 03:31:12 -0800197 void InitializeVoiceDetection()
198 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
peahb624d8c2016-03-05 03:01:14 -0800199 void InitializeEchoCanceller()
200 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700201 int InitializeLocked(const ProcessingConfig& config)
peahdf3efa82015-11-28 12:35:15 -0800202 EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
203
204 // Capture-side exclusive methods possibly running APM in a multi-threaded
205 // manner that are called with the render lock already acquired.
206 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
207 bool output_copy_needed(bool is_data_processed) const
208 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
209 bool is_data_processed() const EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
210 bool synthesis_needed(bool is_data_processed) const
211 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
212 bool analysis_needed(bool is_data_processed) const
213 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
214 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
215
216 // Render-side exclusive methods possibly running APM in a multi-threaded
217 // manner that are called with the render lock already acquired.
ekmeyerson60d9b332015-08-14 10:35:55 -0700218 // TODO(ekm): Remove once all clients updated to new interface.
peahdf3efa82015-11-28 12:35:15 -0800219 int AnalyzeReverseStreamLocked(const float* const* src,
220 const StreamConfig& input_config,
221 const StreamConfig& output_config)
222 EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
223 bool is_rev_processed() const EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
224 int ProcessReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000225
peahdf3efa82015-11-28 12:35:15 -0800226// Debug dump methods that are internal and called without locks.
227// TODO(peah): Make thread safe.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000228#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
229 // TODO(andrew): make this more graceful. Ideally we would split this stuff
230 // out into a separate class with an "enabled" and "disabled" implementation.
peahdf3efa82015-11-28 12:35:15 -0800231 static int WriteMessageToDebugFile(FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -0800232 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -0800233 rtc::CriticalSection* crit_debug,
234 ApmDebugDumpThreadState* debug_state);
235 int WriteInitMessage() EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200236
237 // Writes Config message. If not |forced|, only writes the current config if
238 // it is different from the last saved one; if |forced|, writes the config
239 // regardless of the last saved.
peahdf3efa82015-11-28 12:35:15 -0800240 int WriteConfigMessage(bool forced) EXCLUSIVE_LOCKS_REQUIRED(crit_capture_)
241 EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200242
peahdf3efa82015-11-28 12:35:15 -0800243 // Critical section.
pbos5ad935c2016-01-25 03:52:44 -0800244 rtc::CriticalSection crit_debug_;
Minyue13b96ba2015-10-03 00:39:14 +0200245
peahdf3efa82015-11-28 12:35:15 -0800246 // Debug dump state.
247 ApmDebugDumpState debug_dump_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000248#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
peahdf3efa82015-11-28 12:35:15 -0800250 // Critical sections.
pbos5ad935c2016-01-25 03:52:44 -0800251 rtc::CriticalSection crit_render_ ACQUIRED_BEFORE(crit_capture_);
252 rtc::CriticalSection crit_capture_;
peahdf3efa82015-11-28 12:35:15 -0800253
254 // Structs containing the pointers to the submodules.
kwiberg88788ad2016-02-19 07:04:49 -0800255 std::unique_ptr<ApmPublicSubmodules> public_submodules_;
256 std::unique_ptr<ApmPrivateSubmodules> private_submodules_
peahdf3efa82015-11-28 12:35:15 -0800257 GUARDED_BY(crit_capture_);
258
peah192164e2015-11-17 02:16:45 -0800259 // State that is written to while holding both the render and capture locks
peahdf3efa82015-11-28 12:35:15 -0800260 // but can be read without any lock being held.
261 // As this is only accessed internally of APM, and all internal methods in APM
262 // either are holding the render or capture locks, this construct is safe as
263 // it is not possible to read the variables while writing them.
264 struct ApmFormatState {
265 ApmFormatState()
peah192164e2015-11-17 02:16:45 -0800266 : // Format of processing streams at input/output call sites.
peahdf3efa82015-11-28 12:35:15 -0800267 api_format({{{kSampleRate16kHz, 1, false},
268 {kSampleRate16kHz, 1, false},
269 {kSampleRate16kHz, 1, false},
270 {kSampleRate16kHz, 1, false}}}),
271 rev_proc_format(kSampleRate16kHz, 1) {}
272 ProcessingConfig api_format;
273 StreamConfig rev_proc_format;
274 } formats_;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700275
peahdf3efa82015-11-28 12:35:15 -0800276 // APM constants.
277 const struct ApmConstants {
278 ApmConstants(int agc_startup_min_volume,
peahbe615622016-02-13 16:40:47 -0800279 bool use_experimental_agc,
aluebs2a346882016-01-11 18:04:30 -0800280 bool intelligibility_enabled)
peahdf3efa82015-11-28 12:35:15 -0800281 : // Format of processing streams at input/output call sites.
282 agc_startup_min_volume(agc_startup_min_volume),
peahbe615622016-02-13 16:40:47 -0800283 use_experimental_agc(use_experimental_agc),
aluebs2a346882016-01-11 18:04:30 -0800284 intelligibility_enabled(intelligibility_enabled) {}
peahdf3efa82015-11-28 12:35:15 -0800285 int agc_startup_min_volume;
peahbe615622016-02-13 16:40:47 -0800286 bool use_experimental_agc;
peahdf3efa82015-11-28 12:35:15 -0800287 bool intelligibility_enabled;
peahdf3efa82015-11-28 12:35:15 -0800288 } constants_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289
peahdf3efa82015-11-28 12:35:15 -0800290 struct ApmCaptureState {
aluebs2a346882016-01-11 18:04:30 -0800291 ApmCaptureState(bool transient_suppressor_enabled,
aluebs2a346882016-01-11 18:04:30 -0800292 const std::vector<Point>& array_geometry,
293 SphericalPointf target_direction)
peahdf3efa82015-11-28 12:35:15 -0800294 : aec_system_delay_jumps(-1),
295 delay_offset_ms(0),
296 was_stream_delay_set(false),
297 last_stream_delay_ms(0),
298 last_aec_system_delay_ms(0),
299 stream_delay_jumps(-1),
300 output_will_be_muted(false),
301 key_pressed(false),
302 transient_suppressor_enabled(transient_suppressor_enabled),
aluebs2a346882016-01-11 18:04:30 -0800303 array_geometry(array_geometry),
304 target_direction(target_direction),
peahdf3efa82015-11-28 12:35:15 -0800305 fwd_proc_format(kSampleRate16kHz),
306 split_rate(kSampleRate16kHz) {}
307 int aec_system_delay_jumps;
308 int delay_offset_ms;
309 bool was_stream_delay_set;
310 int last_stream_delay_ms;
311 int last_aec_system_delay_ms;
312 int stream_delay_jumps;
313 bool output_will_be_muted;
314 bool key_pressed;
315 bool transient_suppressor_enabled;
aluebs2a346882016-01-11 18:04:30 -0800316 std::vector<Point> array_geometry;
317 SphericalPointf target_direction;
kwiberg88788ad2016-02-19 07:04:49 -0800318 std::unique_ptr<AudioBuffer> capture_audio;
peahdf3efa82015-11-28 12:35:15 -0800319 // Only the rate and samples fields of fwd_proc_format_ are used because the
320 // forward processing number of channels is mutable and is tracked by the
321 // capture_audio_.
322 StreamConfig fwd_proc_format;
323 int split_rate;
324 } capture_ GUARDED_BY(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000325
peahdf3efa82015-11-28 12:35:15 -0800326 struct ApmCaptureNonLockedState {
aluebsb2328d12016-01-11 20:32:29 -0800327 ApmCaptureNonLockedState(bool beamformer_enabled)
peahdf3efa82015-11-28 12:35:15 -0800328 : fwd_proc_format(kSampleRate16kHz),
329 split_rate(kSampleRate16kHz),
aluebsb2328d12016-01-11 20:32:29 -0800330 stream_delay_ms(0),
331 beamformer_enabled(beamformer_enabled) {}
peahdf3efa82015-11-28 12:35:15 -0800332 // Only the rate and samples fields of fwd_proc_format_ are used because the
333 // forward processing number of channels is mutable and is tracked by the
334 // capture_audio_.
335 StreamConfig fwd_proc_format;
336 int split_rate;
337 int stream_delay_ms;
aluebsb2328d12016-01-11 20:32:29 -0800338 bool beamformer_enabled;
peahdf3efa82015-11-28 12:35:15 -0800339 } capture_nonlocked_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000340
peahdf3efa82015-11-28 12:35:15 -0800341 struct ApmRenderState {
kwiberg88788ad2016-02-19 07:04:49 -0800342 std::unique_ptr<AudioConverter> render_converter;
343 std::unique_ptr<AudioBuffer> render_audio;
peahdf3efa82015-11-28 12:35:15 -0800344 } render_ GUARDED_BY(crit_render_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000345};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000346
niklase@google.com470e71d2011-07-07 08:21:25 +0000347} // namespace webrtc
348
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000349#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_