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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
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 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 32 | assert(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 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 45 | GainControlImpl::GainControlImpl(const AudioProcessing* apm, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 46 | rtc::CriticalSection* crit_render, |
| 47 | rtc::CriticalSection* crit_capture) |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 48 | : ProcessingComponent(), |
| 49 | apm_(apm), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 50 | crit_render_(crit_render), |
| 51 | crit_capture_(crit_capture), |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 52 | mode_(kAdaptiveAnalog), |
| 53 | minimum_capture_level_(0), |
| 54 | maximum_capture_level_(255), |
| 55 | limiter_enabled_(true), |
| 56 | target_level_dbfs_(3), |
| 57 | compression_gain_db_(9), |
| 58 | analog_capture_level_(0), |
| 59 | was_analog_level_set_(false), |
| 60 | stream_is_saturated_(false), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 61 | render_queue_element_max_size_(0) { |
| 62 | RTC_DCHECK(apm); |
| 63 | RTC_DCHECK(crit_render); |
| 64 | RTC_DCHECK(crit_capture); |
| 65 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
| 67 | GainControlImpl::~GainControlImpl() {} |
| 68 | |
| 69 | int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 70 | rtc::CritScope cs(crit_render_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 72 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | } |
| 74 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 75 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 77 | render_queue_buffer_.resize(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | for (int i = 0; i < num_handles(); i++) { |
| 79 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 80 | int err = |
| 81 | WebRtcAgc_GetAddFarendError(my_handle, audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 83 | if (err != AudioProcessing::kNoError) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | return GetHandleError(my_handle); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 85 | |
| 86 | // Buffer the samples in the render queue. |
| 87 | render_queue_buffer_.insert( |
| 88 | render_queue_buffer_.end(), audio->mixed_low_pass_data(), |
| 89 | (audio->mixed_low_pass_data() + audio->num_frames_per_band())); |
| 90 | } |
| 91 | |
| 92 | // Insert the samples into the queue. |
| 93 | if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 94 | // The data queue is full and needs to be emptied. |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 95 | ReadQueuedRenderData(); |
| 96 | |
| 97 | // Retry the insert (should always work). |
| 98 | RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 101 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 104 | // Read chunks of data that were received and queued on the render side from |
| 105 | // a queue. All the data chunks are buffered into the farend signal of the AGC. |
| 106 | void GainControlImpl::ReadQueuedRenderData() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 107 | rtc::CritScope cs(crit_capture_); |
| 108 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 109 | if (!is_component_enabled()) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 114 | int buffer_index = 0; |
| 115 | const int num_frames_per_band = |
| 116 | capture_queue_buffer_.size() / num_handles(); |
| 117 | for (int i = 0; i < num_handles(); i++) { |
| 118 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 119 | WebRtcAgc_AddFarend(my_handle, &capture_queue_buffer_[buffer_index], |
| 120 | num_frames_per_band); |
| 121 | |
| 122 | buffer_index += num_frames_per_band; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 128 | rtc::CritScope cs(crit_capture_); |
| 129 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 131 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 134 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | assert(audio->num_channels() == num_handles()); |
| 136 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 137 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
| 139 | if (mode_ == kAdaptiveAnalog) { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 140 | capture_levels_.assign(num_handles(), analog_capture_level_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | for (int i = 0; i < num_handles(); i++) { |
| 142 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 143 | err = WebRtcAgc_AddMic( |
| 144 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 145 | audio->split_bands(i), |
| 146 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 147 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 149 | if (err != AudioProcessing::kNoError) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | return GetHandleError(my_handle); |
| 151 | } |
| 152 | } |
| 153 | } else if (mode_ == kAdaptiveDigital) { |
| 154 | |
| 155 | for (int i = 0; i < num_handles(); i++) { |
| 156 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 157 | int32_t capture_level_out = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | |
| 159 | err = WebRtcAgc_VirtualMic( |
| 160 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 161 | audio->split_bands(i), |
| 162 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 163 | audio->num_frames_per_band(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | analog_capture_level_, |
| 165 | &capture_level_out); |
| 166 | |
| 167 | capture_levels_[i] = capture_level_out; |
| 168 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 169 | if (err != AudioProcessing::kNoError) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | return GetHandleError(my_handle); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | } |
| 175 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 176 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int GainControlImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 180 | rtc::CritScope cs(crit_capture_); |
| 181 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 183 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (mode_ == kAdaptiveAnalog && !was_analog_level_set_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 187 | return AudioProcessing::kStreamParameterNotSetError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 190 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | assert(audio->num_channels() == num_handles()); |
| 192 | |
| 193 | stream_is_saturated_ = false; |
| 194 | for (int i = 0; i < num_handles(); i++) { |
| 195 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 196 | int32_t capture_level_out = 0; |
| 197 | uint8_t saturation_warning = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 199 | // The call to stream_has_echo() is ok from a deadlock perspective |
| 200 | // as the capture lock is allready held. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | int err = WebRtcAgc_Process( |
| 202 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 203 | audio->split_bands_const(i), |
| 204 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 205 | audio->num_frames_per_band(), |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 206 | audio->split_bands(i), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | capture_levels_[i], |
| 208 | &capture_level_out, |
| 209 | apm_->echo_cancellation()->stream_has_echo(), |
| 210 | &saturation_warning); |
| 211 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 212 | if (err != AudioProcessing::kNoError) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | return GetHandleError(my_handle); |
| 214 | } |
| 215 | |
| 216 | capture_levels_[i] = capture_level_out; |
| 217 | if (saturation_warning == 1) { |
| 218 | stream_is_saturated_ = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (mode_ == kAdaptiveAnalog) { |
| 223 | // Take the analog level to be the average across the handles. |
| 224 | analog_capture_level_ = 0; |
| 225 | for (int i = 0; i < num_handles(); i++) { |
| 226 | analog_capture_level_ += capture_levels_[i]; |
| 227 | } |
| 228 | |
| 229 | analog_capture_level_ /= num_handles(); |
| 230 | } |
| 231 | |
| 232 | was_analog_level_set_ = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 233 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
| 237 | int GainControlImpl::set_stream_analog_level(int level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 238 | rtc::CritScope cs(crit_capture_); |
| 239 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | was_analog_level_set_ = true; |
| 241 | if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 242 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 244 | analog_capture_level_ = level; |
| 245 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 246 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int GainControlImpl::stream_analog_level() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 250 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | // TODO(ajm): enable this assertion? |
| 252 | //assert(mode_ == kAdaptiveAnalog); |
| 253 | |
| 254 | return analog_capture_level_; |
| 255 | } |
| 256 | |
| 257 | int GainControlImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 258 | rtc::CritScope cs_render(crit_render_); |
| 259 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | return EnableComponent(enable); |
| 261 | } |
| 262 | |
| 263 | bool GainControlImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 264 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | return is_component_enabled(); |
| 266 | } |
| 267 | |
| 268 | int GainControlImpl::set_mode(Mode mode) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 269 | rtc::CritScope cs_render(crit_render_); |
| 270 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 271 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 272 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | mode_ = mode; |
| 276 | return Initialize(); |
| 277 | } |
| 278 | |
| 279 | GainControl::Mode GainControlImpl::mode() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 280 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 281 | return mode_; |
| 282 | } |
| 283 | |
| 284 | int GainControlImpl::set_analog_level_limits(int minimum, |
| 285 | int maximum) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 286 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | if (minimum < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 288 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (maximum > 65535) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 292 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (maximum < minimum) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 296 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | minimum_capture_level_ = minimum; |
| 300 | maximum_capture_level_ = maximum; |
| 301 | |
| 302 | return Initialize(); |
| 303 | } |
| 304 | |
| 305 | int GainControlImpl::analog_level_minimum() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 306 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | return minimum_capture_level_; |
| 308 | } |
| 309 | |
| 310 | int GainControlImpl::analog_level_maximum() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 311 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | return maximum_capture_level_; |
| 313 | } |
| 314 | |
| 315 | bool GainControlImpl::stream_is_saturated() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 316 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | return stream_is_saturated_; |
| 318 | } |
| 319 | |
| 320 | int GainControlImpl::set_target_level_dbfs(int level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 321 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | if (level > 31 || level < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 323 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | target_level_dbfs_ = level; |
| 327 | return Configure(); |
| 328 | } |
| 329 | |
| 330 | int GainControlImpl::target_level_dbfs() const { |
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 | return target_level_dbfs_; |
| 333 | } |
| 334 | |
| 335 | int GainControlImpl::set_compression_gain_db(int gain) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 336 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 337 | if (gain < 0 || gain > 90) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 338 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | compression_gain_db_ = gain; |
| 342 | return Configure(); |
| 343 | } |
| 344 | |
| 345 | int GainControlImpl::compression_gain_db() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 346 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | return compression_gain_db_; |
| 348 | } |
| 349 | |
| 350 | int GainControlImpl::enable_limiter(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 351 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | limiter_enabled_ = enable; |
| 353 | return Configure(); |
| 354 | } |
| 355 | |
| 356 | bool GainControlImpl::is_limiter_enabled() 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 limiter_enabled_; |
| 359 | } |
| 360 | |
| 361 | int GainControlImpl::Initialize() { |
| 362 | int err = ProcessingComponent::Initialize(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 363 | if (err != AudioProcessing::kNoError || !is_component_enabled()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 364 | return err; |
| 365 | } |
| 366 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 367 | AllocateRenderQueue(); |
| 368 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 369 | rtc::CritScope cs_capture(crit_capture_); |
kwiberg | cd19faf | 2015-11-05 05:11:19 -0800 | [diff] [blame] | 370 | const int n = num_handles(); |
| 371 | RTC_CHECK_GE(n, 0) << "Bad number of handles: " << n; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 372 | |
kwiberg | cd19faf | 2015-11-05 05:11:19 -0800 | [diff] [blame] | 373 | capture_levels_.assign(n, analog_capture_level_); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 374 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 377 | void GainControlImpl::AllocateRenderQueue() { |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 378 | const size_t new_render_queue_element_max_size = |
| 379 | std::max<size_t>(static_cast<size_t>(1), |
| 380 | kMaxAllowedValuesOfSamplesPerFrame * num_handles()); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 381 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 382 | rtc::CritScope cs_render(crit_render_); |
| 383 | rtc::CritScope cs_capture(crit_capture_); |
| 384 | |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 385 | if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
| 386 | render_queue_element_max_size_ = new_render_queue_element_max_size; |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 387 | std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
| 388 | |
| 389 | render_signal_queue_.reset( |
| 390 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 391 | kMaxNumFramesToBuffer, template_queue_element, |
| 392 | RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 393 | |
| 394 | render_queue_buffer_.resize(render_queue_element_max_size_); |
| 395 | capture_queue_buffer_.resize(render_queue_element_max_size_); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 396 | } else { |
| 397 | render_signal_queue_->Clear(); |
| 398 | } |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 399 | } |
| 400 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | void* GainControlImpl::CreateHandle() const { |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 402 | return WebRtcAgc_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 403 | } |
| 404 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 405 | void GainControlImpl::DestroyHandle(void* handle) const { |
| 406 | WebRtcAgc_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | int GainControlImpl::InitializeHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 410 | rtc::CritScope cs_render(crit_render_); |
| 411 | rtc::CritScope cs_capture(crit_capture_); |
| 412 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | return WebRtcAgc_Init(static_cast<Handle*>(handle), |
| 414 | minimum_capture_level_, |
| 415 | maximum_capture_level_, |
| 416 | MapSetting(mode_), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 417 | apm_->proc_sample_rate_hz()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int GainControlImpl::ConfigureHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 421 | rtc::CritScope cs_render(crit_render_); |
| 422 | rtc::CritScope cs_capture(crit_capture_); |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 423 | WebRtcAgcConfig config; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we |
| 425 | // change the interface. |
| 426 | //assert(target_level_dbfs_ <= 0); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 427 | //config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_); |
| 428 | config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 429 | config.compressionGaindB = |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 430 | static_cast<int16_t>(compression_gain_db_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 431 | config.limiterEnable = limiter_enabled_; |
| 432 | |
| 433 | return WebRtcAgc_set_config(static_cast<Handle*>(handle), config); |
| 434 | } |
| 435 | |
| 436 | int GainControlImpl::num_handles_required() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 437 | // Not locked as it only relies on APM public API which is threadsafe. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | return apm_->num_output_channels(); |
| 439 | } |
| 440 | |
| 441 | int GainControlImpl::GetHandleError(void* handle) const { |
| 442 | // The AGC has no get_error() function. |
| 443 | // (Despite listing errors in its interface...) |
| 444 | assert(handle != NULL); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 445 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | } |
| 447 | } // namespace webrtc |