blob: 0597cd9518531c2789451933cf24488099fe5d47 [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 Graczykc2047542015-07-22 21:06:11 -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;
Michael Graczykdfa36052015-03-25 16:37:27 -070026
27template<typename T>
28class Beamformer;
29
ajm@google.com808e0e02011-08-03 21:08:51 +000030class CriticalSectionWrapper;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000031class EchoCancellationImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000032class EchoControlMobileImpl;
ajm@google.com808e0e02011-08-03 21:08:51 +000033class FileWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000034class GainControlImpl;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000035class GainControlForNewAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +000036class HighPassFilterImpl;
37class LevelEstimatorImpl;
38class NoiseSuppressionImpl;
39class ProcessingComponent;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000040class TransientSuppressor;
niklase@google.com470e71d2011-07-07 08:21:25 +000041class VoiceDetectionImpl;
42
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000043#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
44namespace audioproc {
45
46class Event;
47
48} // namespace audioproc
49#endif
50
niklase@google.com470e71d2011-07-07 08:21:25 +000051class AudioProcessingImpl : public AudioProcessing {
52 public:
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000053 explicit AudioProcessingImpl(const Config& config);
Michael Graczykdfa36052015-03-25 16:37:27 -070054
55 // AudioProcessingImpl takes ownership of beamformer.
56 AudioProcessingImpl(const Config& config, Beamformer<float>* beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +000057 virtual ~AudioProcessingImpl();
58
niklase@google.com470e71d2011-07-07 08:21:25 +000059 // AudioProcessing methods.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000060 int Initialize() override;
61 int Initialize(int input_sample_rate_hz,
62 int output_sample_rate_hz,
63 int reverse_sample_rate_hz,
64 ChannelLayout input_layout,
65 ChannelLayout output_layout,
66 ChannelLayout reverse_layout) override;
Michael Graczykc2047542015-07-22 21:06:11 -070067 int Initialize(const ProcessingConfig& processing_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void SetExtraOptions(const Config& config) override;
69 int set_sample_rate_hz(int rate) override;
70 int input_sample_rate_hz() const override;
71 int sample_rate_hz() const override;
72 int proc_sample_rate_hz() const override;
73 int proc_split_sample_rate_hz() const override;
74 int num_input_channels() const override;
75 int num_output_channels() const override;
76 int num_reverse_channels() const override;
77 void set_output_will_be_muted(bool muted) override;
78 bool output_will_be_muted() const override;
79 int ProcessStream(AudioFrame* frame) override;
80 int ProcessStream(const float* const* src,
81 int samples_per_channel,
82 int input_sample_rate_hz,
83 ChannelLayout input_layout,
84 int output_sample_rate_hz,
85 ChannelLayout output_layout,
86 float* const* dest) override;
Michael Graczykc2047542015-07-22 21:06:11 -070087 int ProcessStream(const float* const* src,
88 const StreamConfig& input_config,
89 const StreamConfig& output_config,
90 float* const* dest) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int AnalyzeReverseStream(AudioFrame* frame) override;
92 int AnalyzeReverseStream(const float* const* data,
93 int samples_per_channel,
94 int sample_rate_hz,
95 ChannelLayout layout) override;
Michael Graczykc2047542015-07-22 21:06:11 -070096 int AnalyzeReverseStream(const float* const* data,
97 const StreamConfig& reverse_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 int set_stream_delay_ms(int delay) override;
99 int stream_delay_ms() const override;
100 bool was_stream_delay_set() const override;
101 void set_delay_offset_ms(int offset) override;
102 int delay_offset_ms() const override;
103 void set_stream_key_pressed(bool key_pressed) override;
104 bool stream_key_pressed() const override;
105 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 Graczykc2047542015-07-22 21:06:11 -0700123 int InitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000124 EXCLUSIVE_LOCKS_REQUIRED(crit_);
Michael Graczykc2047542015-07-22 21:06:11 -0700125 int MaybeInitializeLocked(const ProcessingConfig& config)
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000126 EXCLUSIVE_LOCKS_REQUIRED(crit_);
127 int ProcessStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
128 int AnalyzeReverseStreamLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000129
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000130 bool is_data_processed() const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000131 bool output_copy_needed(bool is_data_processed) const;
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000132 bool synthesis_needed(bool is_data_processed) const;
133 bool analysis_needed(bool is_data_processed) const;
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200134 void InitializeExperimentalAgc() EXCLUSIVE_LOCKS_REQUIRED(crit_);
135 void InitializeTransient() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000136 void InitializeBeamformer() EXCLUSIVE_LOCKS_REQUIRED(crit_);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200137 void MaybeUpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
ajm@google.com808e0e02011-08-03 21:08:51 +0000138
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000139 EchoCancellationImpl* echo_cancellation_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 EchoControlMobileImpl* echo_control_mobile_;
141 GainControlImpl* gain_control_;
142 HighPassFilterImpl* high_pass_filter_;
143 LevelEstimatorImpl* level_estimator_;
144 NoiseSuppressionImpl* noise_suppression_;
145 VoiceDetectionImpl* voice_detection_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000146 rtc::scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
148 std::list<ProcessingComponent*> component_list_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 CriticalSectionWrapper* crit_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000150 rtc::scoped_ptr<AudioBuffer> render_audio_;
151 rtc::scoped_ptr<AudioBuffer> capture_audio_;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000152#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
153 // TODO(andrew): make this more graceful. Ideally we would split this stuff
154 // out into a separate class with an "enabled" and "disabled" implementation.
155 int WriteMessageToDebugFile();
156 int WriteInitMessage();
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000157 rtc::scoped_ptr<FileWrapper> debug_file_;
158 rtc::scoped_ptr<audioproc::Event> event_msg_; // Protobuf message.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000159 std::string event_str_; // Memory for protobuf serialization.
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000160#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Michael Graczykc2047542015-07-22 21:06:11 -0700162 // Format of processing streams at input/output call sites.
163 ProcessingConfig api_format_;
164
165 // Only the rate and samples fields of fwd_proc_format_ are used because the
166 // forward processing number of channels is mutable and is tracked by the
167 // capture_audio_.
168 StreamConfig fwd_proc_format_;
169 StreamConfig rev_proc_format_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000170 int split_rate_;
171
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 int stream_delay_ms_;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000173 int delay_offset_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 bool was_stream_delay_set_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200175 int last_stream_delay_ms_;
176 int last_aec_system_delay_ms_;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200177 int stream_delay_jumps_;
178 int aec_system_delay_jumps_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
Bjorn Volcker424694c2015-03-27 11:30:43 +0100180 bool output_will_be_muted_ GUARDED_BY(crit_);
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000181
182 bool key_pressed_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000183
184 // Only set through the constructor's Config parameter.
185 const bool use_new_agc_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000186 rtc::scoped_ptr<AgcManagerDirect> agc_manager_ GUARDED_BY(crit_);
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200187 int agc_startup_min_volume_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000188
189 bool transient_suppressor_enabled_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000190 rtc::scoped_ptr<TransientSuppressor> transient_suppressor_;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000191 const bool beamformer_enabled_;
Michael Graczykdfa36052015-03-25 16:37:27 -0700192 rtc::scoped_ptr<Beamformer<float>> beamformer_;
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000193 const std::vector<Point> array_geometry_;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000194
195 const bool supports_48kHz_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000197
niklase@google.com470e71d2011-07-07 08:21:25 +0000198} // namespace webrtc
199
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000200#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_PROCESSING_IMPL_H_