Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / content / renderer / media / media_stream_audio_processor.cc
blob168697e20b945e217c530e11acb237d000e04164
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
3 // found in the LICENSE file.
5 #include "content/renderer/media/media_stream_audio_processor.h"
7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram.h"
10 #include "base/trace_event/trace_event.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/renderer/media/media_stream_audio_processor_options.h"
13 #include "content/renderer/media/rtc_media_constraints.h"
14 #include "content/renderer/media/webrtc_audio_device_impl.h"
15 #include "media/audio/audio_parameters.h"
16 #include "media/base/audio_converter.h"
17 #include "media/base/audio_fifo.h"
18 #include "media/base/channel_layout.h"
19 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
20 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
21 #include "third_party/webrtc/modules/audio_processing/typing_detection.h"
23 #if defined(OS_CHROMEOS)
24 #include "base/sys_info.h"
25 #endif
27 namespace content {
29 namespace {
31 using webrtc::AudioProcessing;
32 using webrtc::NoiseSuppression;
34 const int kAudioProcessingNumberOfChannels = 1;
36 AudioProcessing::ChannelLayout MapLayout(media::ChannelLayout media_layout) {
37 switch (media_layout) {
38 case media::CHANNEL_LAYOUT_MONO:
39 return AudioProcessing::kMono;
40 case media::CHANNEL_LAYOUT_STEREO:
41 return AudioProcessing::kStereo;
42 case media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC:
43 return AudioProcessing::kStereoAndKeyboard;
44 default:
45 NOTREACHED() << "Layout not supported: " << media_layout;
46 return AudioProcessing::kMono;
50 // This is only used for playout data where only max two channels is supported.
51 AudioProcessing::ChannelLayout ChannelsToLayout(int num_channels) {
52 switch (num_channels) {
53 case 1:
54 return AudioProcessing::kMono;
55 case 2:
56 return AudioProcessing::kStereo;
57 default:
58 NOTREACHED() << "Channels not supported: " << num_channels;
59 return AudioProcessing::kMono;
63 // Used by UMA histograms and entries shouldn't be re-ordered or removed.
64 enum AudioTrackProcessingStates {
65 AUDIO_PROCESSING_ENABLED = 0,
66 AUDIO_PROCESSING_DISABLED,
67 AUDIO_PROCESSING_IN_WEBRTC,
68 AUDIO_PROCESSING_MAX
71 void RecordProcessingState(AudioTrackProcessingStates state) {
72 UMA_HISTOGRAM_ENUMERATION("Media.AudioTrackProcessingStates",
73 state, AUDIO_PROCESSING_MAX);
76 bool IsDelayAgnosticAecEnabled() {
77 // Note: It's important to query the field trial state first, to ensure that
78 // UMA reports the correct group.
79 const std::string group_name =
80 base::FieldTrialList::FindFullName("UseDelayAgnosticAEC");
81 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
82 if (command_line->HasSwitch(switches::kEnableDelayAgnosticAec))
83 return true;
84 if (command_line->HasSwitch(switches::kDisableDelayAgnosticAec))
85 return false;
87 return (group_name == "Enabled" || group_name == "DefaultEnabled");
90 bool IsBeamformingEnabled(const MediaAudioConstraints& audio_constraints) {
91 return base::FieldTrialList::FindFullName("ChromebookBeamforming") ==
92 "Enabled" ||
93 audio_constraints.GetProperty(MediaAudioConstraints::kGoogBeamforming);
96 void ConfigureBeamforming(webrtc::Config* config,
97 const std::string& geometry_str) {
98 std::vector<webrtc::Point> geometry = ParseArrayGeometry(geometry_str);
99 #if defined(OS_CHROMEOS)
100 if (geometry.empty()) {
101 const std::string& board = base::SysInfo::GetLsbReleaseBoard();
102 if (board.find("nyan_kitty") != std::string::npos) {
103 geometry.push_back(webrtc::Point(-0.03f, 0.f, 0.f));
104 geometry.push_back(webrtc::Point(0.03f, 0.f, 0.f));
105 } else if (board.find("peach_pi") != std::string::npos) {
106 geometry.push_back(webrtc::Point(-0.025f, 0.f, 0.f));
107 geometry.push_back(webrtc::Point(0.025f, 0.f, 0.f));
108 } else if (board.find("samus") != std::string::npos) {
109 geometry.push_back(webrtc::Point(-0.032f, 0.f, 0.f));
110 geometry.push_back(webrtc::Point(0.032f, 0.f, 0.f));
111 } else if (board.find("swanky") != std::string::npos) {
112 geometry.push_back(webrtc::Point(-0.026f, 0.f, 0.f));
113 geometry.push_back(webrtc::Point(0.026f, 0.f, 0.f));
116 #endif
117 config->Set<webrtc::Beamforming>(
118 new webrtc::Beamforming(geometry.size() > 1, geometry));
121 } // namespace
123 // Wraps AudioBus to provide access to the array of channel pointers, since this
124 // is the type webrtc::AudioProcessing deals in. The array is refreshed on every
125 // channel_ptrs() call, and will be valid until the underlying AudioBus pointers
126 // are changed, e.g. through calls to SetChannelData() or SwapChannels().
128 // All methods are called on one of the capture or render audio threads
129 // exclusively.
130 class MediaStreamAudioBus {
131 public:
132 MediaStreamAudioBus(int channels, int frames)
133 : bus_(media::AudioBus::Create(channels, frames)),
134 channel_ptrs_(new float*[channels]) {
135 // May be created in the main render thread and used in the audio threads.
136 thread_checker_.DetachFromThread();
139 media::AudioBus* bus() {
140 DCHECK(thread_checker_.CalledOnValidThread());
141 return bus_.get();
144 float* const* channel_ptrs() {
145 DCHECK(thread_checker_.CalledOnValidThread());
146 for (int i = 0; i < bus_->channels(); ++i) {
147 channel_ptrs_[i] = bus_->channel(i);
149 return channel_ptrs_.get();
152 private:
153 base::ThreadChecker thread_checker_;
154 scoped_ptr<media::AudioBus> bus_;
155 scoped_ptr<float*[]> channel_ptrs_;
158 // Wraps AudioFifo to provide a cleaner interface to MediaStreamAudioProcessor.
159 // It avoids the FIFO when the source and destination frames match. All methods
160 // are called on one of the capture or render audio threads exclusively. If
161 // |source_channels| is larger than |destination_channels|, only the first
162 // |destination_channels| are kept from the source.
163 class MediaStreamAudioFifo {
164 public:
165 MediaStreamAudioFifo(int source_channels,
166 int destination_channels,
167 int source_frames,
168 int destination_frames,
169 int sample_rate)
170 : source_channels_(source_channels),
171 source_frames_(source_frames),
172 sample_rate_(sample_rate),
173 destination_(
174 new MediaStreamAudioBus(destination_channels, destination_frames)),
175 data_available_(false) {
176 DCHECK_GE(source_channels, destination_channels);
177 DCHECK_GT(sample_rate_, 0);
179 if (source_channels > destination_channels) {
180 audio_source_intermediate_ =
181 media::AudioBus::CreateWrapper(destination_channels);
184 if (source_frames != destination_frames) {
185 // Since we require every Push to be followed by as many Consumes as
186 // possible, twice the larger of the two is a (probably) loose upper bound
187 // on the FIFO size.
188 const int fifo_frames = 2 * std::max(source_frames, destination_frames);
189 fifo_.reset(new media::AudioFifo(destination_channels, fifo_frames));
192 // May be created in the main render thread and used in the audio threads.
193 thread_checker_.DetachFromThread();
196 void Push(const media::AudioBus& source, base::TimeDelta audio_delay) {
197 DCHECK(thread_checker_.CalledOnValidThread());
198 DCHECK_EQ(source.channels(), source_channels_);
199 DCHECK_EQ(source.frames(), source_frames_);
201 const media::AudioBus* source_to_push = &source;
203 if (audio_source_intermediate_) {
204 for (int i = 0; i < destination_->bus()->channels(); ++i) {
205 audio_source_intermediate_->SetChannelData(
207 const_cast<float*>(source.channel(i)));
209 audio_source_intermediate_->set_frames(source.frames());
210 source_to_push = audio_source_intermediate_.get();
213 if (fifo_) {
214 CHECK_LT(fifo_->frames(), destination_->bus()->frames());
215 next_audio_delay_ = audio_delay +
216 fifo_->frames() * base::TimeDelta::FromSeconds(1) / sample_rate_;
217 fifo_->Push(source_to_push);
218 } else {
219 CHECK(!data_available_);
220 source_to_push->CopyTo(destination_->bus());
221 next_audio_delay_ = audio_delay;
222 data_available_ = true;
226 // Returns true if there are destination_frames() of data available to be
227 // consumed, and otherwise false.
228 bool Consume(MediaStreamAudioBus** destination,
229 base::TimeDelta* audio_delay) {
230 DCHECK(thread_checker_.CalledOnValidThread());
232 if (fifo_) {
233 if (fifo_->frames() < destination_->bus()->frames())
234 return false;
236 fifo_->Consume(destination_->bus(), 0, destination_->bus()->frames());
237 *audio_delay = next_audio_delay_;
238 next_audio_delay_ -=
239 destination_->bus()->frames() * base::TimeDelta::FromSeconds(1) /
240 sample_rate_;
241 } else {
242 if (!data_available_)
243 return false;
244 *audio_delay = next_audio_delay_;
245 // The data was already copied to |destination_| in this case.
246 data_available_ = false;
249 *destination = destination_.get();
250 return true;
253 private:
254 base::ThreadChecker thread_checker_;
255 const int source_channels_; // For a DCHECK.
256 const int source_frames_; // For a DCHECK.
257 const int sample_rate_;
258 scoped_ptr<media::AudioBus> audio_source_intermediate_;
259 scoped_ptr<MediaStreamAudioBus> destination_;
260 scoped_ptr<media::AudioFifo> fifo_;
262 // When using |fifo_|, this is the audio delay of the first sample to be
263 // consumed next from the FIFO. When not using |fifo_|, this is the audio
264 // delay of the first sample in |destination_|.
265 base::TimeDelta next_audio_delay_;
267 // True when |destination_| contains the data to be returned by the next call
268 // to Consume(). Only used when the FIFO is disabled.
269 bool data_available_;
272 MediaStreamAudioProcessor::MediaStreamAudioProcessor(
273 const blink::WebMediaConstraints& constraints,
274 int effects,
275 WebRtcPlayoutDataSource* playout_data_source)
276 : render_delay_ms_(0),
277 playout_data_source_(playout_data_source),
278 audio_mirroring_(false),
279 typing_detected_(false),
280 stopped_(false) {
281 capture_thread_checker_.DetachFromThread();
282 render_thread_checker_.DetachFromThread();
283 InitializeAudioProcessingModule(constraints, effects);
285 aec_dump_message_filter_ = AecDumpMessageFilter::Get();
286 // In unit tests not creating a message filter, |aec_dump_message_filter_|
287 // will be NULL. We can just ignore that. Other unit tests and browser tests
288 // ensure that we do get the filter when we should.
289 if (aec_dump_message_filter_.get())
290 aec_dump_message_filter_->AddDelegate(this);
293 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() {
294 DCHECK(main_thread_checker_.CalledOnValidThread());
295 Stop();
298 void MediaStreamAudioProcessor::OnCaptureFormatChanged(
299 const media::AudioParameters& input_format) {
300 DCHECK(main_thread_checker_.CalledOnValidThread());
301 // There is no need to hold a lock here since the caller guarantees that
302 // there is no more PushCaptureData() and ProcessAndConsumeData() callbacks
303 // on the capture thread.
304 InitializeCaptureFifo(input_format);
306 // Reset the |capture_thread_checker_| since the capture data will come from
307 // a new capture thread.
308 capture_thread_checker_.DetachFromThread();
311 void MediaStreamAudioProcessor::PushCaptureData(
312 const media::AudioBus& audio_source,
313 base::TimeDelta capture_delay) {
314 DCHECK(capture_thread_checker_.CalledOnValidThread());
316 capture_fifo_->Push(audio_source, capture_delay);
319 bool MediaStreamAudioProcessor::ProcessAndConsumeData(
320 int volume,
321 bool key_pressed,
322 media::AudioBus** processed_data,
323 base::TimeDelta* capture_delay,
324 int* new_volume) {
325 DCHECK(capture_thread_checker_.CalledOnValidThread());
326 DCHECK(processed_data);
327 DCHECK(capture_delay);
328 DCHECK(new_volume);
330 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessAndConsumeData");
332 MediaStreamAudioBus* process_bus;
333 if (!capture_fifo_->Consume(&process_bus, capture_delay))
334 return false;
336 // Use the process bus directly if audio processing is disabled.
337 MediaStreamAudioBus* output_bus = process_bus;
338 *new_volume = 0;
339 if (audio_processing_) {
340 output_bus = output_bus_.get();
341 *new_volume = ProcessData(process_bus->channel_ptrs(),
342 process_bus->bus()->frames(), *capture_delay,
343 volume, key_pressed, output_bus->channel_ptrs());
346 // Swap channels before interleaving the data.
347 if (audio_mirroring_ &&
348 output_format_.channel_layout() == media::CHANNEL_LAYOUT_STEREO) {
349 // Swap the first and second channels.
350 output_bus->bus()->SwapChannels(0, 1);
353 *processed_data = output_bus->bus();
355 return true;
358 void MediaStreamAudioProcessor::Stop() {
359 DCHECK(main_thread_checker_.CalledOnValidThread());
360 if (stopped_)
361 return;
363 stopped_ = true;
365 if (aec_dump_message_filter_.get()) {
366 aec_dump_message_filter_->RemoveDelegate(this);
367 aec_dump_message_filter_ = NULL;
370 if (!audio_processing_.get())
371 return;
373 audio_processing_.get()->UpdateHistogramsOnCallEnd();
374 StopEchoCancellationDump(audio_processing_.get());
376 if (playout_data_source_) {
377 playout_data_source_->RemovePlayoutSink(this);
378 playout_data_source_ = NULL;
382 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const {
383 return input_format_;
386 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const {
387 return output_format_;
390 void MediaStreamAudioProcessor::OnAecDumpFile(
391 const IPC::PlatformFileForTransit& file_handle) {
392 DCHECK(main_thread_checker_.CalledOnValidThread());
394 base::File file = IPC::PlatformFileForTransitToFile(file_handle);
395 DCHECK(file.IsValid());
397 if (audio_processing_)
398 StartEchoCancellationDump(audio_processing_.get(), file.Pass());
399 else
400 file.Close();
403 void MediaStreamAudioProcessor::OnDisableAecDump() {
404 DCHECK(main_thread_checker_.CalledOnValidThread());
405 if (audio_processing_)
406 StopEchoCancellationDump(audio_processing_.get());
409 void MediaStreamAudioProcessor::OnIpcClosing() {
410 DCHECK(main_thread_checker_.CalledOnValidThread());
411 aec_dump_message_filter_ = NULL;
414 void MediaStreamAudioProcessor::OnPlayoutData(media::AudioBus* audio_bus,
415 int sample_rate,
416 int audio_delay_milliseconds) {
417 DCHECK(render_thread_checker_.CalledOnValidThread());
418 DCHECK(audio_processing_->echo_control_mobile()->is_enabled() ^
419 audio_processing_->echo_cancellation()->is_enabled());
421 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::OnPlayoutData");
422 DCHECK_LT(audio_delay_milliseconds,
423 std::numeric_limits<base::subtle::Atomic32>::max());
424 base::subtle::Release_Store(&render_delay_ms_, audio_delay_milliseconds);
426 InitializeRenderFifoIfNeeded(sample_rate, audio_bus->channels(),
427 audio_bus->frames());
429 render_fifo_->Push(
430 *audio_bus, base::TimeDelta::FromMilliseconds(audio_delay_milliseconds));
431 MediaStreamAudioBus* analysis_bus;
432 base::TimeDelta audio_delay;
433 while (render_fifo_->Consume(&analysis_bus, &audio_delay)) {
434 // TODO(ajm): Should AnalyzeReverseStream() account for the |audio_delay|?
435 audio_processing_->AnalyzeReverseStream(
436 analysis_bus->channel_ptrs(),
437 analysis_bus->bus()->frames(),
438 sample_rate,
439 ChannelsToLayout(audio_bus->channels()));
443 void MediaStreamAudioProcessor::OnPlayoutDataSourceChanged() {
444 DCHECK(main_thread_checker_.CalledOnValidThread());
445 // There is no need to hold a lock here since the caller guarantees that
446 // there is no more OnPlayoutData() callback on the render thread.
447 render_thread_checker_.DetachFromThread();
448 render_fifo_.reset();
451 void MediaStreamAudioProcessor::GetStats(AudioProcessorStats* stats) {
452 stats->typing_noise_detected =
453 (base::subtle::Acquire_Load(&typing_detected_) != false);
454 GetAecStats(audio_processing_.get()->echo_cancellation(), stats);
457 void MediaStreamAudioProcessor::InitializeAudioProcessingModule(
458 const blink::WebMediaConstraints& constraints, int effects) {
459 DCHECK(main_thread_checker_.CalledOnValidThread());
460 DCHECK(!audio_processing_);
462 MediaAudioConstraints audio_constraints(constraints, effects);
464 // Audio mirroring can be enabled even though audio processing is otherwise
465 // disabled.
466 audio_mirroring_ = audio_constraints.GetProperty(
467 MediaAudioConstraints::kGoogAudioMirroring);
469 #if defined(OS_IOS)
470 // On iOS, VPIO provides built-in AGC and AEC.
471 const bool echo_cancellation = false;
472 const bool goog_agc = false;
473 #else
474 const bool echo_cancellation =
475 audio_constraints.GetEchoCancellationProperty();
476 const bool goog_agc = audio_constraints.GetProperty(
477 MediaAudioConstraints::kGoogAutoGainControl);
478 #endif
480 #if defined(OS_IOS) || defined(OS_ANDROID)
481 const bool goog_experimental_aec = false;
482 const bool goog_typing_detection = false;
483 #else
484 const bool goog_experimental_aec = audio_constraints.GetProperty(
485 MediaAudioConstraints::kGoogExperimentalEchoCancellation);
486 const bool goog_typing_detection = audio_constraints.GetProperty(
487 MediaAudioConstraints::kGoogTypingNoiseDetection);
488 #endif
490 const bool goog_ns = audio_constraints.GetProperty(
491 MediaAudioConstraints::kGoogNoiseSuppression);
492 const bool goog_experimental_ns = audio_constraints.GetProperty(
493 MediaAudioConstraints::kGoogExperimentalNoiseSuppression);
494 const bool goog_beamforming = IsBeamformingEnabled(audio_constraints);
495 const bool goog_high_pass_filter = audio_constraints.GetProperty(
496 MediaAudioConstraints::kGoogHighpassFilter);
497 // Return immediately if no goog constraint is enabled.
498 if (!echo_cancellation && !goog_experimental_aec && !goog_ns &&
499 !goog_high_pass_filter && !goog_typing_detection &&
500 !goog_agc && !goog_experimental_ns && !goog_beamforming) {
501 RecordProcessingState(AUDIO_PROCESSING_DISABLED);
502 return;
505 // Experimental options provided at creation.
506 webrtc::Config config;
507 if (goog_experimental_aec)
508 config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(true));
509 if (goog_experimental_ns)
510 config.Set<webrtc::ExperimentalNs>(new webrtc::ExperimentalNs(true));
511 if (IsDelayAgnosticAecEnabled())
512 config.Set<webrtc::DelayAgnostic>(new webrtc::DelayAgnostic(true));
513 if (goog_beamforming) {
514 ConfigureBeamforming(&config,
515 audio_constraints.GetPropertyAsString(
516 MediaAudioConstraints::kGoogArrayGeometry));
519 // Create and configure the webrtc::AudioProcessing.
520 audio_processing_.reset(webrtc::AudioProcessing::Create(config));
522 // Enable the audio processing components.
523 if (echo_cancellation) {
524 EnableEchoCancellation(audio_processing_.get());
526 if (playout_data_source_)
527 playout_data_source_->AddPlayoutSink(this);
529 // Prepare for logging echo information. If there are data remaining in
530 // |echo_information_| we simply discard it.
531 echo_information_.reset(new EchoInformation());
534 if (goog_ns) {
535 // The beamforming postfilter is effective at suppressing stationary noise,
536 // so reduce the single-channel NS aggressiveness when enabled.
537 const NoiseSuppression::Level ns_level =
538 config.Get<webrtc::Beamforming>().enabled ? NoiseSuppression::kLow
539 : NoiseSuppression::kHigh;
541 EnableNoiseSuppression(audio_processing_.get(), ns_level);
544 if (goog_high_pass_filter)
545 EnableHighPassFilter(audio_processing_.get());
547 if (goog_typing_detection) {
548 // TODO(xians): Remove this |typing_detector_| after the typing suppression
549 // is enabled by default.
550 typing_detector_.reset(new webrtc::TypingDetection());
551 EnableTypingDetection(audio_processing_.get(), typing_detector_.get());
554 if (goog_agc)
555 EnableAutomaticGainControl(audio_processing_.get());
557 RecordProcessingState(AUDIO_PROCESSING_ENABLED);
560 void MediaStreamAudioProcessor::InitializeCaptureFifo(
561 const media::AudioParameters& input_format) {
562 DCHECK(main_thread_checker_.CalledOnValidThread());
563 DCHECK(input_format.IsValid());
564 input_format_ = input_format;
566 // TODO(ajm): For now, we assume fixed parameters for the output when audio
567 // processing is enabled, to match the previous behavior. We should either
568 // use the input parameters (in which case, audio processing will convert
569 // at output) or ideally, have a backchannel from the sink to know what
570 // format it would prefer.
571 #if defined(OS_ANDROID)
572 int audio_processing_sample_rate = AudioProcessing::kSampleRate16kHz;
573 #else
574 int audio_processing_sample_rate = AudioProcessing::kSampleRate48kHz;
575 #endif
576 const int output_sample_rate = audio_processing_ ?
577 audio_processing_sample_rate :
578 input_format.sample_rate();
579 media::ChannelLayout output_channel_layout = audio_processing_ ?
580 media::GuessChannelLayout(kAudioProcessingNumberOfChannels) :
581 input_format.channel_layout();
583 // The output channels from the fifo is normally the same as input.
584 int fifo_output_channels = input_format.channels();
586 // Special case for if we have a keyboard mic channel on the input and no
587 // audio processing is used. We will then have the fifo strip away that
588 // channel. So we use stereo as output layout, and also change the output
589 // channels for the fifo.
590 if (input_format.channel_layout() ==
591 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC &&
592 !audio_processing_) {
593 output_channel_layout = media::CHANNEL_LAYOUT_STEREO;
594 fifo_output_channels = ChannelLayoutToChannelCount(output_channel_layout);
597 // webrtc::AudioProcessing requires a 10 ms chunk size. We use this native
598 // size when processing is enabled. When disabled we use the same size as
599 // the source if less than 10 ms.
601 // TODO(ajm): This conditional buffer size appears to be assuming knowledge of
602 // the sink based on the source parameters. PeerConnection sinks seem to want
603 // 10 ms chunks regardless, while WebAudio sinks want less, and we're assuming
604 // we can identify WebAudio sinks by the input chunk size. Less fragile would
605 // be to have the sink actually tell us how much it wants (as in the above
606 // TODO).
607 int processing_frames = input_format.sample_rate() / 100;
608 int output_frames = output_sample_rate / 100;
609 if (!audio_processing_ && input_format.frames_per_buffer() < output_frames) {
610 processing_frames = input_format.frames_per_buffer();
611 output_frames = processing_frames;
614 output_format_ = media::AudioParameters(
615 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
616 output_channel_layout,
617 output_sample_rate,
619 output_frames);
621 capture_fifo_.reset(
622 new MediaStreamAudioFifo(input_format.channels(),
623 fifo_output_channels,
624 input_format.frames_per_buffer(),
625 processing_frames,
626 input_format.sample_rate()));
628 if (audio_processing_) {
629 output_bus_.reset(new MediaStreamAudioBus(output_format_.channels(),
630 output_frames));
634 void MediaStreamAudioProcessor::InitializeRenderFifoIfNeeded(
635 int sample_rate, int number_of_channels, int frames_per_buffer) {
636 DCHECK(render_thread_checker_.CalledOnValidThread());
637 if (render_fifo_.get() &&
638 render_format_.sample_rate() == sample_rate &&
639 render_format_.channels() == number_of_channels &&
640 render_format_.frames_per_buffer() == frames_per_buffer) {
641 // Do nothing if the |render_fifo_| has been setup properly.
642 return;
645 render_format_ = media::AudioParameters(
646 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
647 media::GuessChannelLayout(number_of_channels),
648 sample_rate,
650 frames_per_buffer);
652 const int analysis_frames = sample_rate / 100; // 10 ms chunks.
653 render_fifo_.reset(
654 new MediaStreamAudioFifo(number_of_channels,
655 number_of_channels,
656 frames_per_buffer,
657 analysis_frames,
658 sample_rate));
661 int MediaStreamAudioProcessor::ProcessData(const float* const* process_ptrs,
662 int process_frames,
663 base::TimeDelta capture_delay,
664 int volume,
665 bool key_pressed,
666 float* const* output_ptrs) {
667 DCHECK(audio_processing_);
668 DCHECK(capture_thread_checker_.CalledOnValidThread());
670 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessData");
672 base::subtle::Atomic32 render_delay_ms =
673 base::subtle::Acquire_Load(&render_delay_ms_);
674 int64 capture_delay_ms = capture_delay.InMilliseconds();
675 DCHECK_LT(capture_delay_ms,
676 std::numeric_limits<base::subtle::Atomic32>::max());
677 int total_delay_ms = capture_delay_ms + render_delay_ms;
678 if (total_delay_ms > 300) {
679 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms
680 << "ms; render delay: " << render_delay_ms << "ms";
683 webrtc::AudioProcessing* ap = audio_processing_.get();
684 ap->set_stream_delay_ms(total_delay_ms);
686 DCHECK_LE(volume, WebRtcAudioDeviceImpl::kMaxVolumeLevel);
687 webrtc::GainControl* agc = ap->gain_control();
688 int err = agc->set_stream_analog_level(volume);
689 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err;
691 ap->set_stream_key_pressed(key_pressed);
693 err = ap->ProcessStream(process_ptrs,
694 process_frames,
695 input_format_.sample_rate(),
696 MapLayout(input_format_.channel_layout()),
697 output_format_.sample_rate(),
698 MapLayout(output_format_.channel_layout()),
699 output_ptrs);
700 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err;
702 if (typing_detector_) {
703 webrtc::VoiceDetection* vad = ap->voice_detection();
704 DCHECK(vad->is_enabled());
705 bool detected = typing_detector_->Process(key_pressed,
706 vad->stream_has_voice());
707 base::subtle::Release_Store(&typing_detected_, detected);
710 if (echo_information_) {
711 echo_information_.get()->UpdateAecDelayStats(ap->echo_cancellation());
714 // Return 0 if the volume hasn't been changed, and otherwise the new volume.
715 return (agc->stream_analog_level() == volume) ?
716 0 : agc->stream_analog_level();
719 } // namespace content