blob: d82ef020b232b6f965f592c122dd84fcf4499032 [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
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000014#include "webrtc/modules/audio_processing/include/audio_processing.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000015
niklase@google.com470e71d2011-07-07 08:21:25 +000016#include <list>
ajm@google.com808e0e02011-08-03 21:08:51 +000017#include <string>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
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"
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;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000026class Beamformer;
ajm@google.com808e0e02011-08-03 21:08:51 +000027class CriticalSectionWrapper;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000028class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000029class EchoControlMobileImpl;
ajm@google.com808e0e02011-08-03 21:08:51 +000030class FileWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000031class GainControlImpl;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000032class GainControlForNewAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +000033class HighPassFilterImpl;
34class LevelEstimatorImpl;
35class NoiseSuppressionImpl;
36class ProcessingComponent;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037class TransientSuppressor;
niklase@google.com470e71d2011-07-07 08:21:25 +000038class VoiceDetectionImpl;
39
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000040#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
41namespace audioproc {
42
43class Event;
44
45} // namespace audioproc
46#endif
47
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000048class AudioRate {
49 public:
50 explicit AudioRate(int sample_rate_hz)
51 : rate_(sample_rate_hz),
52 samples_per_channel_(AudioProcessing::kChunkSizeMs * rate_ / 1000) {}
53 virtual ~AudioRate() {}
54
55 void set(int rate) {
56 rate_ = rate;
57 samples_per_channel_ = AudioProcessing::kChunkSizeMs * rate_ / 1000;
58 }
59
60 int rate() const { return rate_; }
61 int samples_per_channel() const { return samples_per_channel_; }
62
63 private:
64 int rate_;
65 int samples_per_channel_;
66};
67
68class AudioFormat : public AudioRate {
69 public:
70 AudioFormat(int sample_rate_hz, int num_channels)
71 : AudioRate(sample_rate_hz),
72 num_channels_(num_channels) {}
73 virtual ~AudioFormat() {}
74
75 void set(int rate, int num_channels) {
76 AudioRate::set(rate);
77 num_channels_ = num_channels;
78 }
79
80 int num_channels() const { return num_channels_; }
81
82 private:
83 int num_channels_;
84};
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086class AudioProcessingImpl : public AudioProcessing {
87 public:
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000088 explicit AudioProcessingImpl(const Config& config);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +000089 // Only for testing.
90 AudioProcessingImpl(const Config& config, Beamformer* beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +000091 virtual ~AudioProcessingImpl();
92
niklase@google.com470e71d2011-07-07 08:21:25 +000093 // AudioProcessing methods.
pbos@webrtc.org91620802013-08-02 11:44:11 +000094 virtual int Initialize() OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095 virtual int Initialize(int input_sample_rate_hz,
96 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000097 int reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000098 ChannelLayout input_layout,
99 ChannelLayout output_layout,
100 ChannelLayout reverse_layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000101 virtual void SetExtraOptions(const Config& config) OVERRIDE;
102 virtual int set_sample_rate_hz(int rate) OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000103 virtual int input_sample_rate_hz() const OVERRIDE;
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000104 virtual int sample_rate_hz() const OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000105 virtual int proc_sample_rate_hz() const OVERRIDE;
106 virtual int proc_split_sample_rate_hz() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000107 virtual int num_input_channels() const OVERRIDE;
108 virtual int num_output_channels() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000109 virtual int num_reverse_channels() const OVERRIDE;
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000110 virtual void set_output_will_be_muted(bool muted) OVERRIDE;
111 virtual bool output_will_be_muted() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000112 virtual int ProcessStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 virtual int ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000114 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000116 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117 int output_sample_rate_hz,
118 ChannelLayout output_layout,
119 float* const* dest) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000120 virtual int AnalyzeReverseStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000121 virtual int AnalyzeReverseStream(const float* const* data,
122 int samples_per_channel,
123 int sample_rate_hz,
124 ChannelLayout layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000125 virtual int set_stream_delay_ms(int delay) OVERRIDE;
126 virtual int stream_delay_ms() const OVERRIDE;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000127 virtual bool was_stream_delay_set() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000128 virtual void set_delay_offset_ms(int offset) OVERRIDE;
129 virtual int delay_offset_ms() const OVERRIDE;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000130 virtual void set_stream_key_pressed(bool key_pressed) OVERRIDE;
131 virtual bool stream_key_pressed() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000132 virtual int StartDebugRecording(
133 const char filename[kMaxFilenameSize]) OVERRIDE;
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000134 virtual int StartDebugRecording(FILE* handle) OVERRIDE;
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000135 virtual int StartDebugRecordingForPlatformFile(
136 rtc::PlatformFile handle) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000137 virtual int StopDebugRecording() OVERRIDE;
138 virtual EchoCancellation* echo_cancellation() const OVERRIDE;
139 virtual EchoControlMobile* echo_control_mobile() const OVERRIDE;
140 virtual GainControl* gain_control() const OVERRIDE;
141 virtual HighPassFilter* high_pass_filter() const OVERRIDE;
142 virtual LevelEstimator* level_estimator() const OVERRIDE;
143 virtual NoiseSuppression* noise_suppression() const OVERRIDE;
144 virtual VoiceDetection* voice_detection() const OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000146 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000147 // Overridden in a mock.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000148 virtual int InitializeLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 private:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000151 int InitializeLocked(int input_sample_rate_hz,
152 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000153 int reverse_sample_rate_hz,
154 int num_input_channels,
155 int num_output_channels,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000156 int num_reverse_channels)
157 EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000158 int MaybeInitializeLocked(int input_sample_rate_hz,
159 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000160 int reverse_sample_rate_hz,
161 int num_input_channels,
162 int num_output_channels,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000163 int num_reverse_channels)
164 EXCLUSIVE_LOCKS_REQUIRED(crit_);
165 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
166 int AnalyzeReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000168 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000170 bool synthesis_needed(bool is_data_processed) const;
171 bool analysis_needed(bool is_data_processed) const;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000172 int InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
173 int InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000174 void InitializeBeamformer() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ajm@google.com808e0e02011-08-03 21:08:51 +0000175
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000176 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 EchoControlMobileImpl* echo_control_mobile_;
178 GainControlImpl* gain_control_;
179 HighPassFilterImpl* high_pass_filter_;
180 LevelEstimatorImpl* level_estimator_;
181 NoiseSuppressionImpl* noise_suppression_;
182 VoiceDetectionImpl* voice_detection_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000183 rtc::scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
185 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 CriticalSectionWrapper* crit_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000187 rtc::scoped_ptr<AudioBuffer> render_audio_;
188 rtc::scoped_ptr<AudioBuffer> capture_audio_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000189#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
190 // TODO(andrew): make this more graceful. Ideally we would split this stuff
191 // out into a separate class with an "enabled" and "disabled" implementation.
192 int WriteMessageToDebugFile();
193 int WriteInitMessage();
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000194 rtc::scoped_ptr<FileWrapper> debug_file_;
195 rtc::scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000196 std::string event_str_; // Memory for protobuf serialization.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000197#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000199 AudioFormat fwd_in_format_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000200 // This one is an AudioRate, because the forward processing number of channels
201 // is mutable and is tracked by the capture_audio_.
202 AudioRate fwd_proc_format_;
203 AudioFormat fwd_out_format_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000204 AudioFormat rev_in_format_;
205 AudioFormat rev_proc_format_;
206 int split_rate_;
207
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000209 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 bool was_stream_delay_set_;
211
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000212 bool output_will_be_muted_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000213
214 bool key_pressed_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000215
216 // Only set through the constructor's Config parameter.
217 const bool use_new_agc_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000218 rtc::scoped_ptr<AgcManagerDirect> agc_manager_ GUARDED_BY(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000219
220 bool transient_suppressor_enabled_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000221 rtc::scoped_ptr<TransientSuppressor> transient_suppressor_;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000222 const bool beamformer_enabled_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000223 rtc::scoped_ptr<Beamformer> beamformer_;
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000224 const std::vector<Point> array_geometry_;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000225
226 const bool supports_48kHz_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000228
niklase@google.com470e71d2011-07-07 08:21:25 +0000229} // namespace webrtc
230
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000231#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_