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