blob: 72dfbf419b27ee264d870c22ef5a71330ae4d7e0 [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
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000018#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019#include "webrtc/base/thread_annotations.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070020#include "webrtc/modules/audio_processing/include/audio_processing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000023
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024class AgcManagerDirect;
niklase@google.com470e71d2011-07-07 08:21:25 +000025class AudioBuffer;
ekmeyerson60d9b332015-08-14 10:35:55 -070026class AudioConverter;
Michael Graczykdfa36052015-03-25 16:37:27 -070027
28template<typename T>
29class Beamformer;
30
ajm@google.com808e0e02011-08-03 21:08:51 +000031class CriticalSectionWrapper;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000032class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000033class EchoControlMobileImpl;
ajm@google.com808e0e02011-08-03 21:08:51 +000034class FileWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000035class GainControlImpl;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036class GainControlForNewAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +000037class HighPassFilterImpl;
38class LevelEstimatorImpl;
39class NoiseSuppressionImpl;
40class ProcessingComponent;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000041class TransientSuppressor;
niklase@google.com470e71d2011-07-07 08:21:25 +000042class VoiceDetectionImpl;
ekmeyerson60d9b332015-08-14 10:35:55 -070043class IntelligibilityEnhancer;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000045#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
46namespace audioproc {
47
48class Event;
49
50} // namespace audioproc
51#endif
52
niklase@google.com470e71d2011-07-07 08:21:25 +000053class AudioProcessingImpl : public AudioProcessing {
54 public:
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000055 explicit AudioProcessingImpl(const Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070056
57 // AudioProcessingImpl takes ownership of beamformer.
58 AudioProcessingImpl(const Config& config, Beamformer<float>* beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +000059 virtual ~AudioProcessingImpl();
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061 // AudioProcessing methods.
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;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 int proc_sample_rate_hz() const override;
72 int proc_split_sample_rate_hz() const override;
73 int num_input_channels() const override;
74 int num_output_channels() const override;
75 int num_reverse_channels() const override;
76 void set_output_will_be_muted(bool muted) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000077 int ProcessStream(AudioFrame* frame) override;
78 int ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -070079 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 int input_sample_rate_hz,
81 ChannelLayout input_layout,
82 int output_sample_rate_hz,
83 ChannelLayout output_layout,
84 float* const* dest) override;
Michael Graczyk86c6d332015-07-23 11:41:39 -070085 int ProcessStream(const float* const* src,
86 const StreamConfig& input_config,
87 const StreamConfig& output_config,
88 float* const* dest) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 int AnalyzeReverseStream(AudioFrame* frame) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070090 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -070092 size_t samples_per_channel,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000093 int sample_rate_hz,
94 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070095 int ProcessReverseStream(const float* const* src,
96 const StreamConfig& reverse_input_config,
97 const StreamConfig& reverse_output_config,
98 float* const* dest) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 int set_stream_delay_ms(int delay) override;
100 int stream_delay_ms() const override;
101 bool was_stream_delay_set() const override;
102 void set_delay_offset_ms(int offset) override;
103 int delay_offset_ms() const override;
104 void set_stream_key_pressed(bool key_pressed) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int StartDebugRecording(const char filename[kMaxFilenameSize]) override;
106 int StartDebugRecording(FILE* handle) override;
107 int StartDebugRecordingForPlatformFile(rtc::PlatformFile handle) override;
108 int StopDebugRecording() override;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200109 void UpdateHistogramsOnCallEnd() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 EchoCancellation* echo_cancellation() const override;
111 EchoControlMobile* echo_control_mobile() const override;
112 GainControl* gain_control() const override;
113 HighPassFilter* high_pass_filter() const override;
114 LevelEstimator* level_estimator() const override;
115 NoiseSuppression* noise_suppression() const override;
116 VoiceDetection* voice_detection() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000118 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000119 // Overridden in a mock.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000120 virtual int InitializeLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000121
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 private:
Michael Graczyk86c6d332015-07-23 11:41:39 -0700123 int InitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000124 EXCLUSIVE_LOCKS_REQUIRED(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700125 int MaybeInitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000126 EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700127 // TODO(ekm): Remove once all clients updated to new interface.
128 int AnalyzeReverseStream(const float* const* src,
129 const StreamConfig& input_config,
130 const StreamConfig& output_config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000131 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700132 int ProcessReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000133
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000134 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000135 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000136 bool synthesis_needed(bool is_data_processed) const;
137 bool analysis_needed(bool is_data_processed) const;
ekmeyerson60d9b332015-08-14 10:35:55 -0700138 bool is_rev_processed() const;
139 bool rev_conversion_needed() const;
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200140 void InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
141 void InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000142 void InitializeBeamformer() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700143 void InitializeIntelligibility() EXCLUSIVE_LOCKS_REQUIRED(crit_);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200144 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ajm@google.com808e0e02011-08-03 21:08:51 +0000145
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000146 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 EchoControlMobileImpl* echo_control_mobile_;
148 GainControlImpl* gain_control_;
149 HighPassFilterImpl* high_pass_filter_;
150 LevelEstimatorImpl* level_estimator_;
151 NoiseSuppressionImpl* noise_suppression_;
152 VoiceDetectionImpl* voice_detection_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000153 rtc::scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
155 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 CriticalSectionWrapper* crit_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000157 rtc::scoped_ptr<AudioBuffer> render_audio_;
158 rtc::scoped_ptr<AudioBuffer> capture_audio_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700159 rtc::scoped_ptr<AudioConverter> render_converter_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000160#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
161 // TODO(andrew): make this more graceful. Ideally we would split this stuff
162 // out into a separate class with an "enabled" and "disabled" implementation.
163 int WriteMessageToDebugFile();
164 int WriteInitMessage();
Minyue13b96ba2015-10-03 00:39:14 +0200165
166 // Writes Config message. If not |forced|, only writes the current config if
167 // it is different from the last saved one; if |forced|, writes the config
168 // regardless of the last saved.
169 int WriteConfigMessage(bool forced);
170
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000171 rtc::scoped_ptr<FileWrapper> debug_file_;
172 rtc::scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000173 std::string event_str_; // Memory for protobuf serialization.
Minyue13b96ba2015-10-03 00:39:14 +0200174
175 // Serialized string of last saved APM configuration.
176 std::string last_serialized_config_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000177#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
peah192164e2015-11-17 02:16:45 -0800179 // State that is written to while holding both the render and capture locks
180 // but can be read while holding only one of the locks.
181 struct SharedState {
182 SharedState()
183 : // Format of processing streams at input/output call sites.
184 api_format_({{{kSampleRate16kHz, 1, false},
185 {kSampleRate16kHz, 1, false},
186 {kSampleRate16kHz, 1, false},
187 {kSampleRate16kHz, 1, false}}}) {}
188 ProcessingConfig api_format_;
189 } shared_state_;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700190
191 // Only the rate and samples fields of fwd_proc_format_ are used because the
192 // forward processing number of channels is mutable and is tracked by the
193 // capture_audio_.
194 StreamConfig fwd_proc_format_;
195 StreamConfig rev_proc_format_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000196 int split_rate_;
197
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000199 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 bool was_stream_delay_set_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200201 int last_stream_delay_ms_;
202 int last_aec_system_delay_ms_;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200203 int stream_delay_jumps_;
204 int aec_system_delay_jumps_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
Bjorn Volcker424694c2015-03-27 11:30:43 +0100206 bool output_will_be_muted_ GUARDED_BY(crit_);
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000207
208 bool key_pressed_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000209
210 // Only set through the constructor's Config parameter.
211 const bool use_new_agc_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000212 rtc::scoped_ptr<AgcManagerDirect> agc_manager_ GUARDED_BY(crit_);
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200213 int agc_startup_min_volume_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000214
215 bool transient_suppressor_enabled_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000216 rtc::scoped_ptr<TransientSuppressor> transient_suppressor_;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000217 const bool beamformer_enabled_;
Michael Graczykdfa36052015-03-25 16:37:27 -0700218 rtc::scoped_ptr<Beamformer<float>> beamformer_;
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000219 const std::vector<Point> array_geometry_;
Alejandro Luebscb3f9bd2015-10-29 18:21:34 -0700220 const SphericalPointf target_direction_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700221
222 bool intelligibility_enabled_;
223 rtc::scoped_ptr<IntelligibilityEnhancer> intelligibility_enhancer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000225
niklase@google.com470e71d2011-07-07 08:21:25 +0000226} // namespace webrtc
227
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000228#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_