blob: a08f7b39dd5dc357d2629c105f0e02f949efc221 [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;
71 int set_sample_rate_hz(int rate) override;
72 int input_sample_rate_hz() const override;
73 int sample_rate_hz() const override;
74 int proc_sample_rate_hz() const override;
75 int proc_split_sample_rate_hz() const override;
76 int num_input_channels() const override;
77 int num_output_channels() const override;
78 int num_reverse_channels() const override;
79 void set_output_will_be_muted(bool muted) override;
80 bool output_will_be_muted() const override;
81 int ProcessStream(AudioFrame* frame) override;
82 int ProcessStream(const float* const* src,
83 int samples_per_channel,
84 int input_sample_rate_hz,
85 ChannelLayout input_layout,
86 int output_sample_rate_hz,
87 ChannelLayout output_layout,
88 float* const* dest) override;
Michael Graczyk86c6d332015-07-23 11:41:39 -070089 int ProcessStream(const float* const* src,
90 const StreamConfig& input_config,
91 const StreamConfig& output_config,
92 float* const* dest) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000093 int AnalyzeReverseStream(AudioFrame* frame) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070094 int ProcessReverseStream(AudioFrame* frame) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000095 int AnalyzeReverseStream(const float* const* data,
96 int samples_per_channel,
97 int sample_rate_hz,
98 ChannelLayout layout) override;
ekmeyerson60d9b332015-08-14 10:35:55 -070099 int ProcessReverseStream(const float* const* src,
100 const StreamConfig& reverse_input_config,
101 const StreamConfig& reverse_output_config,
102 float* const* dest) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000103 int set_stream_delay_ms(int delay) override;
104 int stream_delay_ms() const override;
105 bool was_stream_delay_set() const override;
106 void set_delay_offset_ms(int offset) override;
107 int delay_offset_ms() const override;
108 void set_stream_key_pressed(bool key_pressed) override;
109 bool stream_key_pressed() const override;
110 int StartDebugRecording(const char filename[kMaxFilenameSize]) override;
111 int StartDebugRecording(FILE* handle) override;
112 int StartDebugRecordingForPlatformFile(rtc::PlatformFile handle) override;
113 int StopDebugRecording() override;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200114 void UpdateHistogramsOnCallEnd() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 EchoCancellation* echo_cancellation() const override;
116 EchoControlMobile* echo_control_mobile() const override;
117 GainControl* gain_control() const override;
118 HighPassFilter* high_pass_filter() const override;
119 LevelEstimator* level_estimator() const override;
120 NoiseSuppression* noise_suppression() const override;
121 VoiceDetection* voice_detection() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000123 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000124 // Overridden in a mock.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000125 virtual int InitializeLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000126
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 private:
Michael Graczyk86c6d332015-07-23 11:41:39 -0700128 int InitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000129 EXCLUSIVE_LOCKS_REQUIRED(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700130 int MaybeInitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000131 EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700132 // TODO(ekm): Remove once all clients updated to new interface.
133 int AnalyzeReverseStream(const float* const* src,
134 const StreamConfig& input_config,
135 const StreamConfig& output_config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000136 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700137 int ProcessReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000138
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000139 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000140 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000141 bool synthesis_needed(bool is_data_processed) const;
142 bool analysis_needed(bool is_data_processed) const;
ekmeyerson60d9b332015-08-14 10:35:55 -0700143 bool is_rev_processed() const;
144 bool rev_conversion_needed() const;
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200145 void InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
146 void InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000147 void InitializeBeamformer() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700148 void InitializeIntelligibility() EXCLUSIVE_LOCKS_REQUIRED(crit_);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200149 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ajm@google.com808e0e02011-08-03 21:08:51 +0000150
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000151 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 EchoControlMobileImpl* echo_control_mobile_;
153 GainControlImpl* gain_control_;
154 HighPassFilterImpl* high_pass_filter_;
155 LevelEstimatorImpl* level_estimator_;
156 NoiseSuppressionImpl* noise_suppression_;
157 VoiceDetectionImpl* voice_detection_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000158 rtc::scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
160 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 CriticalSectionWrapper* crit_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000162 rtc::scoped_ptr<AudioBuffer> render_audio_;
163 rtc::scoped_ptr<AudioBuffer> capture_audio_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700164 rtc::scoped_ptr<AudioConverter> render_converter_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000165#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
166 // TODO(andrew): make this more graceful. Ideally we would split this stuff
167 // out into a separate class with an "enabled" and "disabled" implementation.
168 int WriteMessageToDebugFile();
169 int WriteInitMessage();
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000170 rtc::scoped_ptr<FileWrapper> debug_file_;
171 rtc::scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000172 std::string event_str_; // Memory for protobuf serialization.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000173#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
Michael Graczyk86c6d332015-07-23 11:41:39 -0700175 // Format of processing streams at input/output call sites.
176 ProcessingConfig api_format_;
177
178 // Only the rate and samples fields of fwd_proc_format_ are used because the
179 // forward processing number of channels is mutable and is tracked by the
180 // capture_audio_.
181 StreamConfig fwd_proc_format_;
182 StreamConfig rev_proc_format_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000183 int split_rate_;
184
niklase@google.com470e71d2011-07-07 08:21:25 +0000185 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000186 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 bool was_stream_delay_set_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200188 int last_stream_delay_ms_;
189 int last_aec_system_delay_ms_;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200190 int stream_delay_jumps_;
191 int aec_system_delay_jumps_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
Bjorn Volcker424694c2015-03-27 11:30:43 +0100193 bool output_will_be_muted_ GUARDED_BY(crit_);
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000194
195 bool key_pressed_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000196
197 // Only set through the constructor's Config parameter.
198 const bool use_new_agc_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000199 rtc::scoped_ptr<AgcManagerDirect> agc_manager_ GUARDED_BY(crit_);
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200200 int agc_startup_min_volume_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000201
202 bool transient_suppressor_enabled_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000203 rtc::scoped_ptr<TransientSuppressor> transient_suppressor_;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000204 const bool beamformer_enabled_;
Michael Graczykdfa36052015-03-25 16:37:27 -0700205 rtc::scoped_ptr<Beamformer<float>> beamformer_;
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000206 const std::vector<Point> array_geometry_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700207
208 bool intelligibility_enabled_;
209 rtc::scoped_ptr<IntelligibilityEnhancer> intelligibility_enhancer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000211
niklase@google.com470e71d2011-07-07 08:21:25 +0000212} // namespace webrtc
213
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000214#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_