Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/webrtc_audio_capturer.h" | 5 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 const int kValidInputRates[] = | 31 const int kValidInputRates[] = |
| 32 {192000, 96000, 48000, 44100, 32000, 16000, 8000}; | 32 {192000, 96000, 48000, 44100, 32000, 16000, 8000}; |
| 33 #elif defined(OS_LINUX) || defined(OS_OPENBSD) | 33 #elif defined(OS_LINUX) || defined(OS_OPENBSD) |
| 34 const int kValidInputRates[] = {48000, 44100}; | 34 const int kValidInputRates[] = {48000, 44100}; |
| 35 #elif defined(OS_ANDROID) | 35 #elif defined(OS_ANDROID) |
| 36 const int kValidInputRates[] = {48000, 44100}; | 36 const int kValidInputRates[] = {48000, 44100}; |
| 37 #else | 37 #else |
| 38 const int kValidInputRates[] = {44100}; | 38 const int kValidInputRates[] = {44100}; |
| 39 #endif | 39 #endif |
| 40 | 40 |
| 41 // Time constant for AudioPowerMonitor. See AudioPowerMonitor ctor comments | |
| 42 // for semantics. This value was arbitrarily chosen, but seems to work well. | |
| 43 const int kPowerMonitorTimeConstantMs = 10; | |
| 44 | |
| 45 // The time between two audio power level samples. | |
| 46 const int kPowerMonitorLogIntervalSeconds = 10; | |
| 47 | |
| 41 } // namespace | 48 } // namespace |
| 42 | 49 |
| 43 // Reference counted container of WebRtcLocalAudioTrack delegate. | 50 // Reference counted container of WebRtcLocalAudioTrack delegate. |
| 44 // TODO(xians): Switch to MediaStreamAudioSinkOwner. | 51 // TODO(xians): Switch to MediaStreamAudioSinkOwner. |
| 45 class WebRtcAudioCapturer::TrackOwner | 52 class WebRtcAudioCapturer::TrackOwner |
| 46 : public base::RefCountedThreadSafe<WebRtcAudioCapturer::TrackOwner> { | 53 : public base::RefCountedThreadSafe<WebRtcAudioCapturer::TrackOwner> { |
| 47 public: | 54 public: |
| 48 explicit TrackOwner(WebRtcLocalAudioTrack* track) | 55 explicit TrackOwner(WebRtcLocalAudioTrack* track) |
| 49 : delegate_(track) {} | 56 : delegate_(track) {} |
| 50 | 57 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 201 |
| 195 // Create and configure the default audio capturing source. | 202 // Create and configure the default audio capturing source. |
| 196 SetCapturerSource(AudioDeviceFactory::NewInputDevice(render_view_id_), | 203 SetCapturerSource(AudioDeviceFactory::NewInputDevice(render_view_id_), |
| 197 channel_layout, | 204 channel_layout, |
| 198 static_cast<float>(device_info_.device.input.sample_rate)); | 205 static_cast<float>(device_info_.device.input.sample_rate)); |
| 199 | 206 |
| 200 // Add the capturer to the WebRtcAudioDeviceImpl since it needs some hardware | 207 // Add the capturer to the WebRtcAudioDeviceImpl since it needs some hardware |
| 201 // information from the capturer. | 208 // information from the capturer. |
| 202 if (audio_device_) | 209 if (audio_device_) |
| 203 audio_device_->AddAudioCapturer(this); | 210 audio_device_->AddAudioCapturer(this); |
| 204 | |
| 205 return true; | 211 return true; |
| 206 } | 212 } |
| 207 | 213 |
| 208 WebRtcAudioCapturer::WebRtcAudioCapturer( | 214 WebRtcAudioCapturer::WebRtcAudioCapturer( |
| 209 int render_view_id, | 215 int render_view_id, |
| 210 const StreamDeviceInfo& device_info, | 216 const StreamDeviceInfo& device_info, |
| 211 const blink::WebMediaConstraints& constraints, | 217 const blink::WebMediaConstraints& constraints, |
| 212 WebRtcAudioDeviceImpl* audio_device) | 218 WebRtcAudioDeviceImpl* audio_device) |
| 213 : constraints_(constraints), | 219 : constraints_(constraints), |
| 214 audio_processor_( | 220 audio_processor_( |
| 215 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( | 221 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( |
| 216 constraints, device_info.device.input.effects, | 222 constraints, device_info.device.input.effects, |
| 217 device_info.device.type, audio_device)), | 223 device_info.device.type, audio_device)), |
| 218 running_(false), | 224 running_(false), |
| 219 render_view_id_(render_view_id), | 225 render_view_id_(render_view_id), |
| 220 device_info_(device_info), | 226 device_info_(device_info), |
| 221 volume_(0), | 227 volume_(0), |
| 222 peer_connection_mode_(false), | 228 peer_connection_mode_(false), |
| 223 key_pressed_(false), | 229 key_pressed_(false), |
| 224 need_audio_processing_(false), | 230 need_audio_processing_(false), |
| 225 audio_device_(audio_device) { | 231 audio_device_(audio_device), |
| 232 audio_power_monitor_( | |
| 233 device_info_.device.input.sample_rate, | |
| 234 base::TimeDelta::FromMilliseconds(kPowerMonitorTimeConstantMs)) { | |
| 226 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; | 235 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; |
| 227 } | 236 } |
| 228 | 237 |
| 229 WebRtcAudioCapturer::~WebRtcAudioCapturer() { | 238 WebRtcAudioCapturer::~WebRtcAudioCapturer() { |
| 230 DCHECK(thread_checker_.CalledOnValidThread()); | 239 DCHECK(thread_checker_.CalledOnValidThread()); |
| 231 DCHECK(tracks_.IsEmpty()); | 240 DCHECK(tracks_.IsEmpty()); |
| 232 DCHECK(!running_); | 241 DCHECK(!running_); |
| 233 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; | 242 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; |
| 234 } | 243 } |
| 235 | 244 |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 | 485 |
| 477 // Notify the tracks on when the format changes. This will do nothing if | 486 // Notify the tracks on when the format changes. This will do nothing if |
| 478 // |tracks_to_notify_format| is empty. | 487 // |tracks_to_notify_format| is empty. |
| 479 media::AudioParameters output_params = audio_processor_->OutputFormat(); | 488 media::AudioParameters output_params = audio_processor_->OutputFormat(); |
| 480 for (TrackList::ItemList::const_iterator it = tracks_to_notify_format.begin(); | 489 for (TrackList::ItemList::const_iterator it = tracks_to_notify_format.begin(); |
| 481 it != tracks_to_notify_format.end(); ++it) { | 490 it != tracks_to_notify_format.end(); ++it) { |
| 482 (*it)->OnSetFormat(output_params); | 491 (*it)->OnSetFormat(output_params); |
| 483 (*it)->SetAudioProcessor(audio_processor_); | 492 (*it)->SetAudioProcessor(audio_processor_); |
| 484 } | 493 } |
| 485 | 494 |
| 495 if ((base::TimeTicks::Now() - last_audio_level_log_time_).InSeconds() > | |
| 496 kPowerMonitorLogIntervalSeconds) { | |
| 497 audio_power_monitor_.Scan(*audio_source, audio_source->frames()); | |
| 498 | |
| 499 last_audio_level_log_time_ = base::TimeTicks::Now(); | |
| 500 | |
| 501 std::pair<float, bool> result = | |
| 502 audio_power_monitor_.ReadCurrentPowerAndClip(); | |
|
tommi (sloooow) - chröme
2014/04/10 09:07:22
nit: I don't suppose there's a single threaded Aud
jiayl
2014/04/10 16:46:34
No, there isn't.
| |
| 503 WebRtcLogMessage(base::StringPrintf( | |
| 504 "WAC::Capture: current_audio_power=%.2fdBFS.", result.first)); | |
|
tommi (sloooow) - chröme
2014/04/10 09:07:22
indent
jiayl
2014/04/10 16:46:34
Done.
| |
| 505 | |
| 506 audio_power_monitor_.Reset(); | |
| 507 } | |
| 508 | |
| 486 // Push the data to the processor for processing. | 509 // Push the data to the processor for processing. |
| 487 audio_processor_->PushCaptureData(audio_source); | 510 audio_processor_->PushCaptureData(audio_source); |
| 488 | 511 |
| 489 // Process and consume the data in the processor until there is not enough | 512 // Process and consume the data in the processor until there is not enough |
| 490 // data in the processor. | 513 // data in the processor. |
| 491 int16* output = NULL; | 514 int16* output = NULL; |
| 492 int new_volume = 0; | 515 int new_volume = 0; |
| 493 while (audio_processor_->ProcessAndConsumeData( | 516 while (audio_processor_->ProcessAndConsumeData( |
| 494 audio_delay, current_volume, key_pressed, &new_volume, &output)) { | 517 audio_delay, current_volume, key_pressed, &new_volume, &output)) { |
| 495 // Feed the post-processed data to the tracks. | 518 // Feed the post-processed data to the tracks. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); | 604 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); |
| 582 audio_processor_->StartAecDump(aec_dump_file); | 605 audio_processor_->StartAecDump(aec_dump_file); |
| 583 } | 606 } |
| 584 | 607 |
| 585 void WebRtcAudioCapturer::StopAecDump() { | 608 void WebRtcAudioCapturer::StopAecDump() { |
| 586 DCHECK(thread_checker_.CalledOnValidThread()); | 609 DCHECK(thread_checker_.CalledOnValidThread()); |
| 587 audio_processor_->StopAecDump(); | 610 audio_processor_->StopAecDump(); |
| 588 } | 611 } |
| 589 | 612 |
| 590 } // namespace content | 613 } // namespace content |
| OLD | NEW |