Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: content/renderer/media/media_stream_audio_processor.cc

Issue 141513006: Wire up AGC to the MediaStreamAudioProcessor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: uploaded again Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/media/media_stream_audio_processor.h" 5 #include "content/renderer/media/media_stream_audio_processor.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "content/public/common/content_switches.h" 9 #include "content/public/common/content_switches.h"
10 #include "content/renderer/media/media_stream_audio_processor_options.h" 10 #include "content/renderer/media/media_stream_audio_processor_options.h"
11 #include "content/renderer/media/rtc_media_constraints.h" 11 #include "content/renderer/media/rtc_media_constraints.h"
12 #include "media/audio/audio_parameters.h" 12 #include "media/audio/audio_parameters.h"
13 #include "media/base/audio_converter.h" 13 #include "media/base/audio_converter.h"
14 #include "media/base/audio_fifo.h" 14 #include "media/base/audio_fifo.h"
15 #include "media/base/channel_layout.h" 15 #include "media/base/channel_layout.h"
16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
17 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface .h" 17 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface .h"
18 18
19 namespace content { 19 namespace content {
20 20
21 namespace { 21 namespace {
22 22
23 using webrtc::AudioProcessing; 23 using webrtc::AudioProcessing;
24 using webrtc::MediaConstraintsInterface; 24 using webrtc::MediaConstraintsInterface;
25 25
26 #if defined(ANDROID) 26 #if defined(ANDROID)
ajm 2014/01/22 21:50:55 What about iOS? I think it is actually appropriate
no longer working on chromium 2014/01/23 12:46:08 WebRtc in Chrome is supported on IOS at all, that
ajm 2014/01/23 17:30:06 Good.
27 const int kAudioProcessingSampleRate = 16000; 27 const int kAudioProcessingSampleRate = 16000;
28 #else 28 #else
29 const int kAudioProcessingSampleRate = 32000; 29 const int kAudioProcessingSampleRate = 32000;
30 #endif 30 #endif
31 const int kAudioProcessingNumberOfChannel = 1; 31 const int kAudioProcessingNumberOfChannel = 1;
32 32
33 const int kMaxNumberOfBuffersInFifo = 2; 33 const int kMaxNumberOfBuffersInFifo = 2;
34 34
35 } // namespace 35 } // namespace
36 36
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // Handles mixing and resampling between input and output parameters. 135 // Handles mixing and resampling between input and output parameters.
136 media::AudioConverter audio_converter_; 136 media::AudioConverter audio_converter_;
137 scoped_ptr<media::AudioBus> audio_wrapper_; 137 scoped_ptr<media::AudioBus> audio_wrapper_;
138 scoped_ptr<media::AudioFifo> fifo_; 138 scoped_ptr<media::AudioFifo> fifo_;
139 }; 139 };
140 140
141 MediaStreamAudioProcessor::MediaStreamAudioProcessor( 141 MediaStreamAudioProcessor::MediaStreamAudioProcessor(
142 const media::AudioParameters& source_params, 142 const media::AudioParameters& source_params,
143 const blink::WebMediaConstraints& constraints, 143 const blink::WebMediaConstraints& constraints,
144 int effects) 144 int effects)
145 : render_delay_ms_(0) { 145 : render_delay_ms_(0),
146 stereo_channels_swapping_(false) {
146 capture_thread_checker_.DetachFromThread(); 147 capture_thread_checker_.DetachFromThread();
147 render_thread_checker_.DetachFromThread(); 148 render_thread_checker_.DetachFromThread();
148 InitializeAudioProcessingModule(constraints, effects); 149 InitializeAudioProcessingModule(constraints, effects);
149 InitializeCaptureConverter(source_params); 150 InitializeCaptureConverter(source_params);
150 } 151 }
151 152
152 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() { 153 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() {
153 DCHECK(main_thread_checker_.CalledOnValidThread()); 154 DCHECK(main_thread_checker_.CalledOnValidThread());
154 StopAudioProcessing(); 155 StopAudioProcessing();
155 } 156 }
(...skipping 22 matching lines...) Expand all
178 base::subtle::Release_Store(&render_delay_ms_, new_render_delay_ms); 179 base::subtle::Release_Store(&render_delay_ms_, new_render_delay_ms);
179 180
180 InitializeRenderConverterIfNeeded(sample_rate, number_of_channels, 181 InitializeRenderConverterIfNeeded(sample_rate, number_of_channels,
181 number_of_frames); 182 number_of_frames);
182 183
183 // TODO(xians): Avoid this extra interleave/deinterleave. 184 // TODO(xians): Avoid this extra interleave/deinterleave.
184 render_data_bus_->FromInterleaved(render_audio, 185 render_data_bus_->FromInterleaved(render_audio,
185 render_data_bus_->frames(), 186 render_data_bus_->frames(),
186 sizeof(render_audio[0])); 187 sizeof(render_audio[0]));
187 render_converter_->Push(render_data_bus_.get()); 188 render_converter_->Push(render_data_bus_.get());
188 while (render_converter_->Convert(&render_frame_)) 189 while (render_converter_->Convert(&render_frame_))
ajm 2014/01/22 21:50:55 FYI, we should be able to handle this conversion i
no longer working on chromium 2014/01/23 12:46:08 That will be awesome. I hope AnalyzeReverseStream
ajm 2014/01/23 17:30:06 I unfortunately didn't mean handle everything ;) I
no longer working on chromium 2014/01/24 09:10:49 That is good thing anyway, please let me know when
189 audio_processing_->AnalyzeReverseStream(&render_frame_); 190 audio_processing_->AnalyzeReverseStream(&render_frame_);
190 } 191 }
191 192
192 bool MediaStreamAudioProcessor::ProcessAndConsumeData( 193 bool MediaStreamAudioProcessor::ProcessAndConsumeData(
193 base::TimeDelta capture_delay, int volume, bool key_pressed, 194 base::TimeDelta capture_delay, int volume, bool key_pressed,
194 int16** out) { 195 int* new_volume, int16** out) {
195 DCHECK(capture_thread_checker_.CalledOnValidThread()); 196 DCHECK(capture_thread_checker_.CalledOnValidThread());
196 TRACE_EVENT0("audio", 197 TRACE_EVENT0("audio",
197 "MediaStreamAudioProcessor::ProcessAndConsumeData"); 198 "MediaStreamAudioProcessor::ProcessAndConsumeData");
199 *new_volume = 0;
198 200
199 if (!capture_converter_->Convert(&capture_frame_)) 201 if (!capture_converter_->Convert(&capture_frame_))
200 return false; 202 return false;
201 203
202 ProcessData(&capture_frame_, capture_delay, volume, key_pressed); 204 *new_volume = ProcessData(&capture_frame_, capture_delay, volume,
205 key_pressed);
203 *out = capture_frame_.data_; 206 *out = capture_frame_.data_;
204 207
205 return true; 208 return true;
206 } 209 }
207 210
208 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const { 211 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const {
209 return capture_converter_->source_parameters(); 212 return capture_converter_->source_parameters();
210 } 213 }
211 214
212 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const { 215 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const {
213 return capture_converter_->sink_parameters(); 216 return capture_converter_->sink_parameters();
214 } 217 }
215 218
216 void MediaStreamAudioProcessor::InitializeAudioProcessingModule( 219 void MediaStreamAudioProcessor::InitializeAudioProcessingModule(
217 const blink::WebMediaConstraints& constraints, int effects) { 220 const blink::WebMediaConstraints& constraints, int effects) {
218 DCHECK(!audio_processing_); 221 DCHECK(!audio_processing_);
219 if (!CommandLine::ForCurrentProcess()->HasSwitch( 222 if (!CommandLine::ForCurrentProcess()->HasSwitch(
220 switches::kEnableAudioTrackProcessing)) { 223 switches::kEnableAudioTrackProcessing)) {
221 return; 224 return;
222 } 225 }
223 226
224 RTCMediaConstraints native_constraints(constraints); 227 RTCMediaConstraints native_constraints(constraints);
225 ApplyFixedAudioConstraints(&native_constraints); 228 ApplyFixedAudioConstraints(&native_constraints);
226 if (effects & media::AudioParameters::ECHO_CANCELLER) { 229 if (effects & media::AudioParameters::ECHO_CANCELLER) {
ajm 2014/01/22 21:50:55 How does this interact with the code in media_stre
no longer working on chromium 2014/01/23 12:46:08 Those code in media_stream_dependency_factory will
ajm 2014/01/23 17:30:06 Sounds good.
227 // If platform echo cancellator is enabled, disable the software AEC. 230 // If platform echo cancellator is enabled, disable the software AEC.
ajm 2014/01/22 21:50:55 cancellator -> canceller
no longer working on chromium 2014/01/23 12:46:08 done
228 native_constraints.AddMandatory( 231 native_constraints.AddMandatory(
229 MediaConstraintsInterface::kEchoCancellation, 232 MediaConstraintsInterface::kEchoCancellation,
230 MediaConstraintsInterface::kValueFalse, true); 233 MediaConstraintsInterface::kValueFalse, true);
231 } 234 }
232 235
233 const bool enable_aec = GetPropertyFromConstraints( 236 const bool enable_aec = GetPropertyFromConstraints(
234 &native_constraints, MediaConstraintsInterface::kEchoCancellation); 237 &native_constraints, MediaConstraintsInterface::kEchoCancellation);
235 const bool enable_ns = GetPropertyFromConstraints( 238 const bool enable_ns = GetPropertyFromConstraints(
236 &native_constraints, MediaConstraintsInterface::kNoiseSuppression); 239 &native_constraints, MediaConstraintsInterface::kNoiseSuppression);
237 const bool enable_high_pass_filter = GetPropertyFromConstraints( 240 const bool enable_high_pass_filter = GetPropertyFromConstraints(
238 &native_constraints, MediaConstraintsInterface::kHighpassFilter); 241 &native_constraints, MediaConstraintsInterface::kHighpassFilter);
239 #if defined(IOS) || defined(ANDROID) 242 #if defined(IOS) || defined(ANDROID)
ajm 2014/01/22 21:50:55 Set enable_agc = false here as well.
no longer working on chromium 2014/01/23 12:46:08 Done, thanks.
240 const bool enable_experimental_aec = false; 243 const bool enable_experimental_aec = false;
241 const bool enable_typing_detection = false; 244 const bool enable_typing_detection = false;
242 #else 245 #else
243 const bool enable_experimental_aec = GetPropertyFromConstraints( 246 const bool enable_experimental_aec = GetPropertyFromConstraints(
244 &native_constraints, 247 &native_constraints,
245 MediaConstraintsInterface::kExperimentalEchoCancellation); 248 MediaConstraintsInterface::kExperimentalEchoCancellation);
246 const bool enable_typing_detection = GetPropertyFromConstraints( 249 const bool enable_typing_detection = GetPropertyFromConstraints(
247 &native_constraints, MediaConstraintsInterface::kTypingNoiseDetection); 250 &native_constraints, MediaConstraintsInterface::kTypingNoiseDetection);
248 #endif 251 #endif
252 const bool enable_agc = GetPropertyFromConstraints(
253 &native_constraints, webrtc::MediaConstraintsInterface::kAutoGainControl);
254 // TODO(xians): Comments in mediachannel.h claim that SetAgcConfig is only
255 // available for old AGC, check with ajm@ to see if we support
256 // set_target_level_dbfs(), set_compression_gain_db() and enable_limiter()
257 // from the constraints.
no longer working on chromium 2014/01/21 09:05:47 Andrew, do you know if SetAgcConfig is needed or n
ajm 2014/01/22 21:50:55 No, we don't support these currently. You can remo
no longer working on chromium 2014/01/23 12:46:08 Done.
258
259 stereo_channels_swapping_ = GetPropertyFromConstraints(
260 &native_constraints, webrtc::MediaConstraintsInterface::kAudioMirroring);
249 261
250 // Return immediately if no audio processing component is enabled. 262 // Return immediately if no audio processing component is enabled.
251 if (!enable_aec && !enable_experimental_aec && !enable_ns && 263 if (!enable_aec && !enable_experimental_aec && !enable_ns &&
252 !enable_high_pass_filter && !enable_typing_detection) { 264 !enable_high_pass_filter && !enable_typing_detection && !enable_agc) {
253 return; 265 return;
254 } 266 }
255 267
256 // Create and configure the webrtc::AudioProcessing. 268 // Create and configure the webrtc::AudioProcessing.
257 audio_processing_.reset(webrtc::AudioProcessing::Create(0)); 269 audio_processing_.reset(webrtc::AudioProcessing::Create(0));
258 270
259 // Enable the audio processing components. 271 // Enable the audio processing components.
260 if (enable_aec) { 272 if (enable_aec) {
261 EnableEchoCancellation(audio_processing_.get()); 273 EnableEchoCancellation(audio_processing_.get());
262 if (enable_experimental_aec) 274 if (enable_experimental_aec)
263 EnableExperimentalEchoCancellation(audio_processing_.get()); 275 EnableExperimentalEchoCancellation(audio_processing_.get());
264 } 276 }
265 277
266 if (enable_ns) 278 if (enable_ns)
267 EnableNoiseSuppression(audio_processing_.get()); 279 EnableNoiseSuppression(audio_processing_.get());
268 280
269 if (enable_high_pass_filter) 281 if (enable_high_pass_filter)
270 EnableHighPassFilter(audio_processing_.get()); 282 EnableHighPassFilter(audio_processing_.get());
271 283
272 if (enable_typing_detection) 284 if (enable_typing_detection)
273 EnableTypingDetection(audio_processing_.get()); 285 EnableTypingDetection(audio_processing_.get());
274 286
287 if (enable_agc)
288 EnableAutomaticGainControl(audio_processing_.get());
275 289
276 // Configure the audio format the audio processing is running on. This 290 // Configure the audio format the audio processing is running on. This
277 // has to be done after all the needed components are enabled. 291 // has to be done after all the needed components are enabled.
ajm 2014/01/22 21:50:55 Why is that? Did you hit an error? Typically it wo
no longer working on chromium 2014/01/23 12:46:08 I never hit any problem here. I think the reason w
278 CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate), 292 CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate),
279 0); 293 0);
280 CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, 294 CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel,
281 kAudioProcessingNumberOfChannel), 295 kAudioProcessingNumberOfChannel),
282 0); 296 0);
283 } 297 }
284 298
285 void MediaStreamAudioProcessor::InitializeCaptureConverter( 299 void MediaStreamAudioProcessor::InitializeCaptureConverter(
286 const media::AudioParameters& source_params) { 300 const media::AudioParameters& source_params) {
287 DCHECK(source_params.IsValid()); 301 DCHECK(source_params.IsValid());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 media::AudioParameters sink_params( 348 media::AudioParameters sink_params(
335 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 349 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
336 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16, 350 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16,
337 kAudioProcessingSampleRate / 100); 351 kAudioProcessingSampleRate / 100);
338 render_converter_.reset( 352 render_converter_.reset(
339 new MediaStreamAudioConverter(source_params, sink_params)); 353 new MediaStreamAudioConverter(source_params, sink_params));
340 render_data_bus_ = media::AudioBus::Create(number_of_channels, 354 render_data_bus_ = media::AudioBus::Create(number_of_channels,
341 frames_per_buffer); 355 frames_per_buffer);
342 } 356 }
343 357
344 void MediaStreamAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame, 358 int MediaStreamAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame,
345 base::TimeDelta capture_delay, 359 base::TimeDelta capture_delay,
346 int volume, 360 int volume,
347 bool key_pressed) { 361 bool key_pressed) {
348 DCHECK(capture_thread_checker_.CalledOnValidThread()); 362 DCHECK(capture_thread_checker_.CalledOnValidThread());
349 if (!audio_processing_) 363 if (!audio_processing_)
350 return; 364 return 0;
351 365
352 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::Process10MsData"); 366 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessData");
353 DCHECK_EQ(audio_processing_->sample_rate_hz(), 367 DCHECK_EQ(audio_processing_->sample_rate_hz(),
354 capture_converter_->sink_parameters().sample_rate()); 368 capture_converter_->sink_parameters().sample_rate());
355 DCHECK_EQ(audio_processing_->num_input_channels(), 369 DCHECK_EQ(audio_processing_->num_input_channels(),
356 capture_converter_->sink_parameters().channels()); 370 capture_converter_->sink_parameters().channels());
357 DCHECK_EQ(audio_processing_->num_output_channels(), 371 DCHECK_EQ(audio_processing_->num_output_channels(),
358 capture_converter_->sink_parameters().channels()); 372 capture_converter_->sink_parameters().channels());
359 373
360 base::subtle::Atomic32 render_delay_ms = 374 base::subtle::Atomic32 render_delay_ms =
361 base::subtle::Acquire_Load(&render_delay_ms_); 375 base::subtle::Acquire_Load(&render_delay_ms_);
362 int64 capture_delay_ms = capture_delay.InMilliseconds(); 376 int64 capture_delay_ms = capture_delay.InMilliseconds();
363 DCHECK_LT(capture_delay_ms, 377 DCHECK_LT(capture_delay_ms,
364 std::numeric_limits<base::subtle::Atomic32>::max()); 378 std::numeric_limits<base::subtle::Atomic32>::max());
365 int total_delay_ms = capture_delay_ms + render_delay_ms; 379 int total_delay_ms = capture_delay_ms + render_delay_ms;
366 if (total_delay_ms > 1000) { 380 if (total_delay_ms > 1000) {
ajm 2014/01/22 21:50:55 In voice engine, this was a lower value (300 ms I
no longer working on chromium 2014/01/23 12:46:08 This is from old code in WebRtcAudioDeviceImpl, bu
367 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms 381 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms
368 << "ms; render delay: " << render_delay_ms << "ms"; 382 << "ms; render delay: " << render_delay_ms << "ms";
369 } 383 }
370 384
371 audio_processing_->set_stream_delay_ms(total_delay_ms); 385 audio_processing_->set_stream_delay_ms(total_delay_ms);
372 webrtc::GainControl* agc = audio_processing_->gain_control(); 386 webrtc::GainControl* agc = audio_processing_->gain_control();
387 // TODO(xians): We used to have a problem with the truncation of the volume.
388 // For example, if the OS has 25 volume steps, and the current volume is 7,
389 // which will be scaled to 70 in [0, 255]. When the AGC tries to adjust to
390 // volume to 76, SetVolume will fail updating the volume due to truncating
391 // the new volume back to 7. WebRtc works around this problem by keeping
392 // track values and forcing AGC to continue its trend.
393 // Check with ajm@ on if we still need the workaround.
373 int err = agc->set_stream_analog_level(volume); 394 int err = agc->set_stream_analog_level(volume);
no longer working on chromium 2014/01/21 09:05:47 Andrew, could you please take a look at this comme
ajm 2014/01/23 17:30:06 As discussed off review, you can remove this TODO.
no longer working on chromium 2014/01/24 09:10:49 Done. Thanks.
374 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err; 395 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err;
375 err = audio_processing_->ProcessStream(audio_frame); 396 err = audio_processing_->ProcessStream(audio_frame);
376 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err; 397 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err;
377 398
378 // TODO(xians): Add support for AGC, typing detection, audio level 399 // TODO(xians): Add support for typing detection, audio level calculation.
379 // calculation, stereo swapping. 400
401 if (stereo_channels_swapping_ && audio_frame->num_channels_ == 2) {
402 // TODO(xians): Swap the stereo channels after switching to media::AudioBus.
403 }
404
405 // Return 0 if the volume has not been changed, otherwise return the new
406 // volume.
407 return (agc->stream_analog_level() == volume) ?
408 0 : agc->stream_analog_level();
380 } 409 }
381 410
382 void MediaStreamAudioProcessor::StopAudioProcessing() { 411 void MediaStreamAudioProcessor::StopAudioProcessing() {
383 if (!audio_processing_.get()) 412 if (!audio_processing_.get())
384 return; 413 return;
385 414
386 audio_processing_.reset(); 415 audio_processing_.reset();
387 } 416 }
388 417
389 } // namespace content 418 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698