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