blob: 3a0ad2402aae8fcd421c66018d14d72a17f95d47 [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
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_
13
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.org7fad4b82013-05-28 08:11:59 +000019#include "webrtc/system_wrappers/interface/scoped_ptr.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023class AudioBuffer;
ajm@google.com808e0e02011-08-03 21:08:51 +000024class CriticalSectionWrapper;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000025class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000026class EchoControlMobileImpl;
ajm@google.com808e0e02011-08-03 21:08:51 +000027class FileWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000028class GainControlImpl;
29class HighPassFilterImpl;
30class LevelEstimatorImpl;
31class NoiseSuppressionImpl;
32class ProcessingComponent;
33class VoiceDetectionImpl;
34
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000035#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
36namespace audioproc {
37
38class Event;
39
40} // namespace audioproc
41#endif
42
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000043class AudioRate {
44 public:
45 explicit AudioRate(int sample_rate_hz)
46 : rate_(sample_rate_hz),
47 samples_per_channel_(AudioProcessing::kChunkSizeMs * rate_ / 1000) {}
48 virtual ~AudioRate() {}
49
50 void set(int rate) {
51 rate_ = rate;
52 samples_per_channel_ = AudioProcessing::kChunkSizeMs * rate_ / 1000;
53 }
54
55 int rate() const { return rate_; }
56 int samples_per_channel() const { return samples_per_channel_; }
57
58 private:
59 int rate_;
60 int samples_per_channel_;
61};
62
63class AudioFormat : public AudioRate {
64 public:
65 AudioFormat(int sample_rate_hz, int num_channels)
66 : AudioRate(sample_rate_hz),
67 num_channels_(num_channels) {}
68 virtual ~AudioFormat() {}
69
70 void set(int rate, int num_channels) {
71 AudioRate::set(rate);
72 num_channels_ = num_channels;
73 }
74
75 int num_channels() const { return num_channels_; }
76
77 private:
78 int num_channels_;
79};
80
niklase@google.com470e71d2011-07-07 08:21:25 +000081class AudioProcessingImpl : public AudioProcessing {
82 public:
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000083 explicit AudioProcessingImpl(const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000084 virtual ~AudioProcessingImpl();
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086 // AudioProcessing methods.
pbos@webrtc.org91620802013-08-02 11:44:11 +000087 virtual int Initialize() OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000088 virtual int Initialize(int input_sample_rate_hz,
89 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090 int reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091 ChannelLayout input_layout,
92 ChannelLayout output_layout,
93 ChannelLayout reverse_layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +000094 virtual void SetExtraOptions(const Config& config) OVERRIDE;
aluebs@webrtc.org0b72f582013-11-19 15:17:51 +000095 virtual int EnableExperimentalNs(bool enable) OVERRIDE;
96 virtual bool experimental_ns_enabled() const OVERRIDE {
97 return false;
98 }
pbos@webrtc.org91620802013-08-02 11:44:11 +000099 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;
101 virtual int proc_sample_rate_hz() const OVERRIDE;
102 virtual int proc_split_sample_rate_hz() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000103 virtual int num_input_channels() const OVERRIDE;
104 virtual int num_output_channels() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000105 virtual int num_reverse_channels() const OVERRIDE;
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000106 virtual void set_output_will_be_muted(bool muted) OVERRIDE;
107 virtual bool output_will_be_muted() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000108 virtual int ProcessStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000109 virtual int ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000110 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000111 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000112 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 int output_sample_rate_hz,
114 ChannelLayout output_layout,
115 float* const* dest) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000116 virtual int AnalyzeReverseStream(AudioFrame* frame) OVERRIDE;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000117 virtual int AnalyzeReverseStream(const float* const* data,
118 int samples_per_channel,
119 int sample_rate_hz,
120 ChannelLayout layout) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000121 virtual int set_stream_delay_ms(int delay) OVERRIDE;
122 virtual int stream_delay_ms() const OVERRIDE;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000123 virtual bool was_stream_delay_set() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000124 virtual void set_delay_offset_ms(int offset) OVERRIDE;
125 virtual int delay_offset_ms() const OVERRIDE;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000126 virtual void set_stream_key_pressed(bool key_pressed) OVERRIDE;
127 virtual bool stream_key_pressed() const OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000128 virtual int StartDebugRecording(
129 const char filename[kMaxFilenameSize]) OVERRIDE;
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000130 virtual int StartDebugRecording(FILE* handle) OVERRIDE;
pbos@webrtc.org91620802013-08-02 11:44:11 +0000131 virtual int StopDebugRecording() OVERRIDE;
132 virtual EchoCancellation* echo_cancellation() const OVERRIDE;
133 virtual EchoControlMobile* echo_control_mobile() const OVERRIDE;
134 virtual GainControl* gain_control() const OVERRIDE;
135 virtual HighPassFilter* high_pass_filter() const OVERRIDE;
136 virtual LevelEstimator* level_estimator() const OVERRIDE;
137 virtual NoiseSuppression* noise_suppression() const OVERRIDE;
138 virtual VoiceDetection* voice_detection() const OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000140 protected:
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000141 // Overridden in a mock.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000142 virtual int InitializeLocked();
143
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 private:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000145 int InitializeLocked(int input_sample_rate_hz,
146 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000147 int reverse_sample_rate_hz,
148 int num_input_channels,
149 int num_output_channels,
150 int num_reverse_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000151 int MaybeInitializeLocked(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,
156 int num_reverse_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000157 int ProcessStreamLocked();
158 int AnalyzeReverseStreamLocked();
159
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000160 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000161 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000162 bool synthesis_needed(bool is_data_processed) const;
163 bool analysis_needed(bool is_data_processed) const;
ajm@google.com808e0e02011-08-03 21:08:51 +0000164
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000165 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 EchoControlMobileImpl* echo_control_mobile_;
167 GainControlImpl* gain_control_;
168 HighPassFilterImpl* high_pass_filter_;
169 LevelEstimatorImpl* level_estimator_;
170 NoiseSuppressionImpl* noise_suppression_;
171 VoiceDetectionImpl* voice_detection_;
172
173 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 CriticalSectionWrapper* crit_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000175 scoped_ptr<AudioBuffer> render_audio_;
176 scoped_ptr<AudioBuffer> capture_audio_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000177#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
178 // TODO(andrew): make this more graceful. Ideally we would split this stuff
179 // out into a separate class with an "enabled" and "disabled" implementation.
180 int WriteMessageToDebugFile();
181 int WriteInitMessage();
182 scoped_ptr<FileWrapper> debug_file_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000183 scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
184 std::string event_str_; // Memory for protobuf serialization.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000185#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000187 AudioFormat fwd_in_format_;
188 AudioFormat fwd_proc_format_;
189 AudioRate fwd_out_format_;
190 AudioFormat rev_in_format_;
191 AudioFormat rev_proc_format_;
192 int split_rate_;
193
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000195 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 bool was_stream_delay_set_;
197
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000198 bool output_will_be_muted_;
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000199
200 bool key_pressed_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000202
niklase@google.com470e71d2011-07-07 08:21:25 +0000203} // namespace webrtc
204
205#endif // WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_