niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 13 | #include "webrtc/base/constructormagic.h" |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 14 | #include "webrtc/base/optional.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
bjornv@webrtc.org | b395a5e | 2014-12-16 10:38:10 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_processing/agc/legacy/gain_control.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
| 20 | typedef void Handle; |
| 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 23 | int16_t MapSetting(GainControl::Mode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | switch (mode) { |
| 25 | case GainControl::kAdaptiveAnalog: |
| 26 | return kAgcModeAdaptiveAnalog; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | case GainControl::kAdaptiveDigital: |
| 28 | return kAgcModeAdaptiveDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | case GainControl::kFixedDigital: |
| 30 | return kAgcModeFixedDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 32 | RTC_DCHECK(false); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 33 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 36 | // Maximum length that a frame of samples can have. |
| 37 | static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; |
| 38 | // Maximum number of frames to buffer in the render queue. |
| 39 | // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 40 | // reverse and forward call numbers. |
| 41 | static const size_t kMaxNumFramesToBuffer = 100; |
| 42 | |
| 43 | } // namespace |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 44 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 45 | class GainControlImpl::GainController { |
| 46 | public: |
| 47 | explicit GainController() { |
| 48 | state_ = WebRtcAgc_Create(); |
| 49 | RTC_CHECK(state_); |
| 50 | } |
| 51 | |
| 52 | ~GainController() { |
| 53 | RTC_DCHECK(state_); |
| 54 | WebRtcAgc_Free(state_); |
| 55 | } |
| 56 | |
| 57 | Handle* state() { |
| 58 | RTC_DCHECK(state_); |
| 59 | return state_; |
| 60 | } |
| 61 | |
| 62 | void Initialize(int minimum_capture_level, |
| 63 | int maximum_capture_level, |
| 64 | Mode mode, |
| 65 | int sample_rate_hz, |
| 66 | int capture_level) { |
| 67 | RTC_DCHECK(state_); |
| 68 | int error = |
| 69 | WebRtcAgc_Init(state_, minimum_capture_level, maximum_capture_level, |
| 70 | MapSetting(mode), sample_rate_hz); |
| 71 | RTC_DCHECK_EQ(0, error); |
| 72 | |
| 73 | set_capture_level(capture_level); |
| 74 | } |
| 75 | |
| 76 | void set_capture_level(int capture_level) { |
| 77 | capture_level_ = rtc::Optional<int>(capture_level); |
| 78 | } |
| 79 | |
| 80 | int get_capture_level() { |
| 81 | RTC_DCHECK(capture_level_); |
| 82 | return *capture_level_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | Handle* state_; |
| 87 | // TODO(peah): Remove the optional once the initialization is moved into the |
| 88 | // ctor. |
| 89 | rtc::Optional<int> capture_level_; |
| 90 | |
| 91 | RTC_DISALLOW_COPY_AND_ASSIGN(GainController); |
| 92 | }; |
| 93 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 94 | GainControlImpl::GainControlImpl(rtc::CriticalSection* crit_render, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 95 | rtc::CriticalSection* crit_capture) |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 96 | : crit_render_(crit_render), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 97 | crit_capture_(crit_capture), |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 98 | mode_(kAdaptiveAnalog), |
| 99 | minimum_capture_level_(0), |
| 100 | maximum_capture_level_(255), |
| 101 | limiter_enabled_(true), |
| 102 | target_level_dbfs_(3), |
| 103 | compression_gain_db_(9), |
| 104 | analog_capture_level_(0), |
| 105 | was_analog_level_set_(false), |
| 106 | stream_is_saturated_(false), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 107 | render_queue_element_max_size_(0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 108 | RTC_DCHECK(crit_render); |
| 109 | RTC_DCHECK(crit_capture); |
| 110 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
| 112 | GainControlImpl::~GainControlImpl() {} |
| 113 | |
| 114 | int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 115 | rtc::CritScope cs(crit_render_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 116 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 117 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 120 | RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 122 | render_queue_buffer_.resize(0); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 123 | for (auto& gain_controller : gain_controllers_) { |
| 124 | int err = WebRtcAgc_GetAddFarendError(gain_controller->state(), |
| 125 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 127 | if (err != AudioProcessing::kNoError) { |
| 128 | return AudioProcessing::kUnspecifiedError; |
| 129 | } |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 130 | |
| 131 | // Buffer the samples in the render queue. |
| 132 | render_queue_buffer_.insert( |
| 133 | render_queue_buffer_.end(), audio->mixed_low_pass_data(), |
| 134 | (audio->mixed_low_pass_data() + audio->num_frames_per_band())); |
| 135 | } |
| 136 | |
| 137 | // Insert the samples into the queue. |
| 138 | if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 139 | // The data queue is full and needs to be emptied. |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 140 | ReadQueuedRenderData(); |
| 141 | |
| 142 | // Retry the insert (should always work). |
| 143 | RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 146 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 149 | // Read chunks of data that were received and queued on the render side from |
| 150 | // a queue. All the data chunks are buffered into the farend signal of the AGC. |
| 151 | void GainControlImpl::ReadQueuedRenderData() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 152 | rtc::CritScope cs(crit_capture_); |
| 153 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 154 | if (!enabled_) { |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 159 | size_t buffer_index = 0; |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 160 | RTC_DCHECK(num_proc_channels_); |
| 161 | RTC_DCHECK_LT(0ul, *num_proc_channels_); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 162 | const size_t num_frames_per_band = |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 163 | capture_queue_buffer_.size() / (*num_proc_channels_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 164 | for (auto& gain_controller : gain_controllers_) { |
| 165 | WebRtcAgc_AddFarend(gain_controller->state(), |
| 166 | &capture_queue_buffer_[buffer_index], |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 167 | num_frames_per_band); |
| 168 | |
| 169 | buffer_index += num_frames_per_band; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 175 | rtc::CritScope cs(crit_capture_); |
| 176 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 177 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 178 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 181 | RTC_DCHECK(num_proc_channels_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 182 | RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 183 | RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_); |
| 184 | RTC_DCHECK_LE(*num_proc_channels_, gain_controllers_.size()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
| 186 | if (mode_ == kAdaptiveAnalog) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 187 | int capture_channel = 0; |
| 188 | for (auto& gain_controller : gain_controllers_) { |
| 189 | gain_controller->set_capture_level(analog_capture_level_); |
| 190 | int err = WebRtcAgc_AddMic( |
| 191 | gain_controller->state(), audio->split_bands(capture_channel), |
| 192 | audio->num_bands(), audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 194 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 195 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 197 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | } |
| 199 | } else if (mode_ == kAdaptiveDigital) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 200 | int capture_channel = 0; |
| 201 | for (auto& gain_controller : gain_controllers_) { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 202 | int32_t capture_level_out = 0; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 203 | int err = WebRtcAgc_VirtualMic( |
| 204 | gain_controller->state(), audio->split_bands(capture_channel), |
| 205 | audio->num_bands(), audio->num_frames_per_band(), |
| 206 | analog_capture_level_, &capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 208 | gain_controller->set_capture_level(capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 210 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 211 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 213 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 217 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | } |
| 219 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 220 | int GainControlImpl::ProcessCaptureAudio(AudioBuffer* audio, |
| 221 | bool stream_has_echo) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 222 | rtc::CritScope cs(crit_capture_); |
| 223 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 224 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 225 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (mode_ == kAdaptiveAnalog && !was_analog_level_set_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 229 | return AudioProcessing::kStreamParameterNotSetError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 232 | RTC_DCHECK(num_proc_channels_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 233 | RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 234 | RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | |
| 236 | stream_is_saturated_ = false; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 237 | int capture_channel = 0; |
| 238 | for (auto& gain_controller : gain_controllers_) { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 239 | int32_t capture_level_out = 0; |
| 240 | uint8_t saturation_warning = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 242 | // The call to stream_has_echo() is ok from a deadlock perspective |
| 243 | // as the capture lock is allready held. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 244 | int err = WebRtcAgc_Process( |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 245 | gain_controller->state(), audio->split_bands_const(capture_channel), |
| 246 | audio->num_bands(), audio->num_frames_per_band(), |
| 247 | audio->split_bands(capture_channel), |
| 248 | gain_controller->get_capture_level(), &capture_level_out, |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 249 | stream_has_echo, &saturation_warning); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 251 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 252 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 255 | gain_controller->set_capture_level(capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | if (saturation_warning == 1) { |
| 257 | stream_is_saturated_ = true; |
| 258 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 259 | |
| 260 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | } |
| 262 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 263 | RTC_DCHECK_LT(0ul, *num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | if (mode_ == kAdaptiveAnalog) { |
| 265 | // Take the analog level to be the average across the handles. |
| 266 | analog_capture_level_ = 0; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 267 | for (auto& gain_controller : gain_controllers_) { |
| 268 | analog_capture_level_ += gain_controller->get_capture_level(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | } |
| 270 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 271 | analog_capture_level_ /= (*num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | was_analog_level_set_ = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 275 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | } |
| 277 | |
aluebs | 11d4a42 | 2016-04-28 14:58:32 -0700 | [diff] [blame] | 278 | int GainControlImpl::compression_gain_db() const { |
| 279 | rtc::CritScope cs(crit_capture_); |
| 280 | return compression_gain_db_; |
| 281 | } |
| 282 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
| 284 | int GainControlImpl::set_stream_analog_level(int level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 285 | rtc::CritScope cs(crit_capture_); |
| 286 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | was_analog_level_set_ = true; |
| 288 | if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 289 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 291 | analog_capture_level_ = level; |
| 292 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 293 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | int GainControlImpl::stream_analog_level() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 297 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 298 | // TODO(ajm): enable this assertion? |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame^] | 299 | //RTC_DCHECK_EQ(kAdaptiveAnalog, mode_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | |
| 301 | return analog_capture_level_; |
| 302 | } |
| 303 | |
| 304 | int GainControlImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 305 | rtc::CritScope cs_render(crit_render_); |
| 306 | rtc::CritScope cs_capture(crit_capture_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 307 | if (enable && !enabled_) { |
| 308 | enabled_ = enable; // Must be set before Initialize() is called. |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 309 | |
| 310 | RTC_DCHECK(num_proc_channels_); |
| 311 | RTC_DCHECK(sample_rate_hz_); |
| 312 | Initialize(*num_proc_channels_, *sample_rate_hz_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 313 | } else { |
| 314 | enabled_ = enable; |
| 315 | } |
| 316 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | bool GainControlImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 320 | rtc::CritScope cs(crit_capture_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 321 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | int GainControlImpl::set_mode(Mode mode) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 325 | rtc::CritScope cs_render(crit_render_); |
| 326 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 327 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 328 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | mode_ = mode; |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 332 | RTC_DCHECK(num_proc_channels_); |
| 333 | RTC_DCHECK(sample_rate_hz_); |
| 334 | Initialize(*num_proc_channels_, *sample_rate_hz_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 335 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | GainControl::Mode GainControlImpl::mode() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 339 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | return mode_; |
| 341 | } |
| 342 | |
| 343 | int GainControlImpl::set_analog_level_limits(int minimum, |
| 344 | int maximum) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | if (minimum < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 346 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (maximum > 65535) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 350 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (maximum < minimum) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 354 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
peah | 7c931ad | 2016-03-24 12:52:02 -0700 | [diff] [blame] | 357 | size_t num_proc_channels_local = 0u; |
| 358 | int sample_rate_hz_local = 0; |
| 359 | { |
| 360 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 361 | |
peah | 7c931ad | 2016-03-24 12:52:02 -0700 | [diff] [blame] | 362 | minimum_capture_level_ = minimum; |
| 363 | maximum_capture_level_ = maximum; |
| 364 | |
| 365 | RTC_DCHECK(num_proc_channels_); |
| 366 | RTC_DCHECK(sample_rate_hz_); |
| 367 | num_proc_channels_local = *num_proc_channels_; |
| 368 | sample_rate_hz_local = *sample_rate_hz_; |
| 369 | } |
| 370 | Initialize(num_proc_channels_local, sample_rate_hz_local); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 371 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | int GainControlImpl::analog_level_minimum() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 375 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | return minimum_capture_level_; |
| 377 | } |
| 378 | |
| 379 | int GainControlImpl::analog_level_maximum() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 380 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 381 | return maximum_capture_level_; |
| 382 | } |
| 383 | |
| 384 | bool GainControlImpl::stream_is_saturated() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 385 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 386 | return stream_is_saturated_; |
| 387 | } |
| 388 | |
| 389 | int GainControlImpl::set_target_level_dbfs(int level) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | if (level > 31 || level < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 391 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 393 | { |
| 394 | rtc::CritScope cs(crit_capture_); |
| 395 | target_level_dbfs_ = level; |
| 396 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 397 | return Configure(); |
| 398 | } |
| 399 | |
| 400 | int GainControlImpl::target_level_dbfs() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 401 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | return target_level_dbfs_; |
| 403 | } |
| 404 | |
| 405 | int GainControlImpl::set_compression_gain_db(int gain) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 406 | if (gain < 0 || gain > 90) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 407 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 408 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 409 | { |
| 410 | rtc::CritScope cs(crit_capture_); |
| 411 | compression_gain_db_ = gain; |
| 412 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | return Configure(); |
| 414 | } |
| 415 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 416 | int GainControlImpl::enable_limiter(bool enable) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 417 | { |
| 418 | rtc::CritScope cs(crit_capture_); |
| 419 | limiter_enabled_ = enable; |
| 420 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | return Configure(); |
| 422 | } |
| 423 | |
| 424 | bool GainControlImpl::is_limiter_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 425 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 426 | return limiter_enabled_; |
| 427 | } |
| 428 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 429 | void GainControlImpl::Initialize(size_t num_proc_channels, int sample_rate_hz) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 430 | rtc::CritScope cs_render(crit_render_); |
| 431 | rtc::CritScope cs_capture(crit_capture_); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 432 | |
| 433 | num_proc_channels_ = rtc::Optional<size_t>(num_proc_channels); |
| 434 | sample_rate_hz_ = rtc::Optional<int>(sample_rate_hz); |
| 435 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 436 | if (!enabled_) { |
| 437 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | } |
| 439 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 440 | gain_controllers_.resize(*num_proc_channels_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 441 | for (auto& gain_controller : gain_controllers_) { |
| 442 | if (!gain_controller) { |
| 443 | gain_controller.reset(new GainController()); |
| 444 | } |
| 445 | gain_controller->Initialize(minimum_capture_level_, maximum_capture_level_, |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 446 | mode_, *sample_rate_hz_, analog_capture_level_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | Configure(); |
| 450 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 451 | AllocateRenderQueue(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 452 | } |
| 453 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 454 | void GainControlImpl::AllocateRenderQueue() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 455 | rtc::CritScope cs_render(crit_render_); |
| 456 | rtc::CritScope cs_capture(crit_capture_); |
| 457 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 458 | RTC_DCHECK(num_proc_channels_); |
| 459 | const size_t new_render_queue_element_max_size = std::max<size_t>( |
| 460 | static_cast<size_t>(1), |
| 461 | kMaxAllowedValuesOfSamplesPerFrame * (*num_proc_channels_)); |
| 462 | |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 463 | if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
| 464 | render_queue_element_max_size_ = new_render_queue_element_max_size; |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 465 | std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
| 466 | |
| 467 | render_signal_queue_.reset( |
| 468 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 469 | kMaxNumFramesToBuffer, template_queue_element, |
| 470 | RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 471 | |
| 472 | render_queue_buffer_.resize(render_queue_element_max_size_); |
| 473 | capture_queue_buffer_.resize(render_queue_element_max_size_); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 474 | } else { |
| 475 | render_signal_queue_->Clear(); |
| 476 | } |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 477 | } |
| 478 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 479 | int GainControlImpl::Configure() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 480 | rtc::CritScope cs_render(crit_render_); |
| 481 | rtc::CritScope cs_capture(crit_capture_); |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 482 | WebRtcAgcConfig config; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 483 | // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we |
| 484 | // change the interface. |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame^] | 485 | //RTC_DCHECK_LE(target_level_dbfs_, 0); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 486 | //config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_); |
| 487 | config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 488 | config.compressionGaindB = |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 489 | static_cast<int16_t>(compression_gain_db_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 490 | config.limiterEnable = limiter_enabled_; |
| 491 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 492 | int error = AudioProcessing::kNoError; |
| 493 | for (auto& gain_controller : gain_controllers_) { |
| 494 | const int handle_error = |
| 495 | WebRtcAgc_set_config(gain_controller->state(), config); |
| 496 | if (handle_error != AudioProcessing::kNoError) { |
| 497 | error = handle_error; |
| 498 | } |
| 499 | } |
| 500 | return error; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 501 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 502 | } // namespace webrtc |