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