blob: be70273e65a4ab73b9322404cb2a9b07e6545513 [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
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000020#include "webrtc/system_wrappers/interface/scoped_ptr.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;
ajm@google.com808e0e02011-08-03 21:08:51 +000026class CriticalSectionWrapper;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000027class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000028class EchoControlMobileImpl;
ajm@google.com808e0e02011-08-03 21:08:51 +000029class FileWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000030class GainControlImpl;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000031class GainControlForNewAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +000032class HighPassFilterImpl;
33class LevelEstimatorImpl;
34class NoiseSuppressionImpl;
35class ProcessingComponent;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036class TransientSuppressor;
niklase@google.com470e71d2011-07-07 08:21:25 +000037class VoiceDetectionImpl;
38
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000039#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
40namespace audioproc {
41
42class Event;
43
44} // namespace audioproc
45#endif
46
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000047class AudioRate {
48 public:
49 explicit AudioRate(int sample_rate_hz)
50 : rate_(sample_rate_hz),
51 samples_per_channel_(AudioProcessing::kChunkSizeMs * rate_ / 1000) {}
52 virtual ~AudioRate() {}
53
54 void set(int rate) {
55 rate_ = rate;
56 samples_per_channel_ = AudioProcessing::kChunkSizeMs * rate_ / 1000;
57 }
58
59 int rate() const { return rate_; }
60 int samples_per_channel() const { return samples_per_channel_; }
61
62 private:
63 int rate_;
64 int samples_per_channel_;
65};
66
67class AudioFormat : public AudioRate {
68 public:
69 AudioFormat(int sample_rate_hz, int num_channels)
70 : AudioRate(sample_rate_hz),
71 num_channels_(num_channels) {}
72 virtual ~AudioFormat() {}
73
74 void set(int rate, int num_channels) {
75 AudioRate::set(rate);
76 num_channels_ = num_channels;
77 }
78
79 int num_channels() const { return num_channels_; }
80
81 private:
82 int num_channels_;
83};
84
niklase@google.com470e71d2011-07-07 08:21:25 +000085class AudioProcessingImpl : public AudioProcessing {
86 public:
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000087 explicit AudioProcessingImpl(const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000088 virtual ~AudioProcessingImpl();
89
niklase@google.com470e71d2011-07-07 08:21:25 +000090 // AudioProcessing methods.
pbos@webrtc.org91620802013-08-02 11:44:11 +000091 virtual int Initialize() OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092 virtual int Initialize(int input_sample_rate_hz,
93 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000094 int reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095 ChannelLayout input_layout,
96 ChannelLayout output_layout,
97 ChannelLayout reverse_layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +000098 virtual void SetExtraOptions(const Config& config) OVERRIDE;
99 virtual int set_sample_rate_hz(int rate) OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000100 virtual int input_sample_rate_hz() const OVERRIDE;
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000101 virtual int sample_rate_hz() const OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000102 virtual int proc_sample_rate_hz() const OVERRIDE;
103 virtual int proc_split_sample_rate_hz() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000104 virtual int num_input_channels() const OVERRIDE;
105 virtual int num_output_channels() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000106 virtual int num_reverse_channels() const OVERRIDE;
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000107 virtual void set_output_will_be_muted(bool muted) OVERRIDE;
108 virtual bool output_will_be_muted() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000109 virtual int ProcessStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000110 virtual int ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000111 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000112 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000113 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 int output_sample_rate_hz,
115 ChannelLayout output_layout,
116 float* const* dest) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000117 virtual int AnalyzeReverseStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000118 virtual int AnalyzeReverseStream(const float* const* data,
119 int samples_per_channel,
120 int sample_rate_hz,
121 ChannelLayout layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000122 virtual int set_stream_delay_ms(int delay) OVERRIDE;
123 virtual int stream_delay_ms() const OVERRIDE;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000124 virtual bool was_stream_delay_set() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000125 virtual void set_delay_offset_ms(int offset) OVERRIDE;
126 virtual int delay_offset_ms() const OVERRIDE;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000127 virtual void set_stream_key_pressed(bool key_pressed) OVERRIDE;
128 virtual bool stream_key_pressed() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000129 virtual int StartDebugRecording(
130 const char filename[kMaxFilenameSize]) OVERRIDE;
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000131 virtual int StartDebugRecording(FILE* handle) OVERRIDE;
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000132 virtual int StartDebugRecordingForPlatformFile(
133 rtc::PlatformFile handle) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000134 virtual int StopDebugRecording() OVERRIDE;
135 virtual EchoCancellation* echo_cancellation() const OVERRIDE;
136 virtual EchoControlMobile* echo_control_mobile() const OVERRIDE;
137 virtual GainControl* gain_control() const OVERRIDE;
138 virtual HighPassFilter* high_pass_filter() const OVERRIDE;
139 virtual LevelEstimator* level_estimator() const OVERRIDE;
140 virtual NoiseSuppression* noise_suppression() const OVERRIDE;
141 virtual VoiceDetection* voice_detection() const OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000143 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000144 // Overridden in a mock.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000145 virtual int InitializeLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000146
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 private:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000148 int InitializeLocked(int input_sample_rate_hz,
149 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000150 int reverse_sample_rate_hz,
151 int num_input_channels,
152 int num_output_channels,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000153 int num_reverse_channels)
154 EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000155 int MaybeInitializeLocked(int input_sample_rate_hz,
156 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000157 int reverse_sample_rate_hz,
158 int num_input_channels,
159 int num_output_channels,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000160 int num_reverse_channels)
161 EXCLUSIVE_LOCKS_REQUIRED(crit_);
162 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
163 int AnalyzeReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000165 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000167 bool synthesis_needed(bool is_data_processed) const;
168 bool analysis_needed(bool is_data_processed) const;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000169 int InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
170 int InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ajm@google.com808e0e02011-08-03 21:08:51 +0000171
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000172 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 EchoControlMobileImpl* echo_control_mobile_;
174 GainControlImpl* gain_control_;
175 HighPassFilterImpl* high_pass_filter_;
176 LevelEstimatorImpl* level_estimator_;
177 NoiseSuppressionImpl* noise_suppression_;
178 VoiceDetectionImpl* voice_detection_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000179 scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
181 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 CriticalSectionWrapper* crit_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000183 scoped_ptr<AudioBuffer> render_audio_;
184 scoped_ptr<AudioBuffer> capture_audio_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000185#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
186 // TODO(andrew): make this more graceful. Ideally we would split this stuff
187 // out into a separate class with an "enabled" and "disabled" implementation.
188 int WriteMessageToDebugFile();
189 int WriteInitMessage();
190 scoped_ptr<FileWrapper> debug_file_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000191 scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
192 std::string event_str_; // Memory for protobuf serialization.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000193#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000195 AudioFormat fwd_in_format_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000196 // This one is an AudioRate, because the forward processing number of channels
197 // is mutable and is tracked by the capture_audio_.
198 AudioRate fwd_proc_format_;
199 AudioFormat fwd_out_format_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000200 AudioFormat rev_in_format_;
201 AudioFormat rev_proc_format_;
202 int split_rate_;
203
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000205 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 bool was_stream_delay_set_;
207
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000208 bool output_will_be_muted_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000209
210 bool key_pressed_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000211
212 // Only set through the constructor's Config parameter.
213 const bool use_new_agc_;
214 scoped_ptr<AgcManagerDirect> agc_manager_ GUARDED_BY(crit_);
215
216 bool transient_suppressor_enabled_;
217 scoped_ptr<TransientSuppressor> transient_suppressor_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000219
niklase@google.com470e71d2011-07-07 08:21:25 +0000220} // namespace webrtc
221
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000222#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_