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 "media/audio/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 SyncWriter* sync_writer, | 56 SyncWriter* sync_writer, |
57 UserInputMonitor* user_input_monitor) | 57 UserInputMonitor* user_input_monitor) |
58 : creator_task_runner_(base::MessageLoopProxy::current()), | 58 : creator_task_runner_(base::MessageLoopProxy::current()), |
59 handler_(handler), | 59 handler_(handler), |
60 stream_(NULL), | 60 stream_(NULL), |
61 data_is_active_(false), | 61 data_is_active_(false), |
62 state_(CLOSED), | 62 state_(CLOSED), |
63 sync_writer_(sync_writer), | 63 sync_writer_(sync_writer), |
64 max_volume_(0.0), | 64 max_volume_(0.0), |
65 user_input_monitor_(user_input_monitor), | 65 user_input_monitor_(user_input_monitor), |
| 66 #if defined(AUDIO_POWER_MONITORING) |
| 67 last_audio_level_was_silence_(false), |
| 68 silence_counter_(0), |
| 69 #endif |
66 prev_key_down_count_(0) { | 70 prev_key_down_count_(0) { |
67 DCHECK(creator_task_runner_.get()); | 71 DCHECK(creator_task_runner_.get()); |
68 } | 72 } |
69 | 73 |
70 AudioInputController::~AudioInputController() { | 74 AudioInputController::~AudioInputController() { |
71 DCHECK_EQ(state_, CLOSED); | 75 DCHECK_EQ(state_, CLOSED); |
72 } | 76 } |
73 | 77 |
74 // static | 78 // static |
75 scoped_refptr<AudioInputController> AudioInputController::Create( | 79 scoped_refptr<AudioInputController> AudioInputController::Create( |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 200 |
197 #if defined(AUDIO_POWER_MONITORING) | 201 #if defined(AUDIO_POWER_MONITORING) |
198 // Create the audio (power) level meter given the provided audio parameters. | 202 // Create the audio (power) level meter given the provided audio parameters. |
199 // An AudioBus is also needed to wrap the raw data buffer from the native | 203 // An AudioBus is also needed to wrap the raw data buffer from the native |
200 // layer to match AudioPowerMonitor::Scan(). | 204 // layer to match AudioPowerMonitor::Scan(). |
201 // TODO(henrika): Remove use of extra AudioBus. See http://crbug.com/375155. | 205 // TODO(henrika): Remove use of extra AudioBus. See http://crbug.com/375155. |
202 audio_level_.reset(new media::AudioPowerMonitor( | 206 audio_level_.reset(new media::AudioPowerMonitor( |
203 params.sample_rate(), | 207 params.sample_rate(), |
204 TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMilliseconds))); | 208 TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMilliseconds))); |
205 audio_params_ = params; | 209 audio_params_ = params; |
| 210 last_audio_level_was_silence_ = false; |
| 211 silence_counter_ = 0; |
206 #endif | 212 #endif |
207 | 213 |
208 // TODO(miu): See TODO at top of file. Until that's resolved, assume all | 214 // TODO(miu): See TODO at top of file. Until that's resolved, assume all |
209 // platform audio input requires the |no_data_timer_| be used to auto-detect | 215 // platform audio input requires the |no_data_timer_| be used to auto-detect |
210 // errors. In reality, probably only Windows needs to be treated as | 216 // errors. In reality, probably only Windows needs to be treated as |
211 // unreliable here. | 217 // unreliable here. |
212 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id), | 218 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id), |
213 true); | 219 true); |
214 } | 220 } |
215 | 221 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 | 306 |
301 DoStopCloseAndClearStream(); | 307 DoStopCloseAndClearStream(); |
302 SetDataIsActive(false); | 308 SetDataIsActive(false); |
303 | 309 |
304 if (SharedMemoryAndSyncSocketMode()) | 310 if (SharedMemoryAndSyncSocketMode()) |
305 sync_writer_->Close(); | 311 sync_writer_->Close(); |
306 | 312 |
307 if (user_input_monitor_) | 313 if (user_input_monitor_) |
308 user_input_monitor_->DisableKeyPressMonitoring(); | 314 user_input_monitor_->DisableKeyPressMonitoring(); |
309 | 315 |
| 316 #if defined(AUDIO_POWER_MONITORING) |
| 317 // Send UMA stats if we have enabled power monitoring. |
| 318 if (audio_level_) { |
| 319 UMA_HISTOGRAM_COUNTS_10000("Media.AudioInputControllerSilenceCount", |
| 320 silence_counter_); |
| 321 } |
| 322 #endif |
| 323 |
310 state_ = CLOSED; | 324 state_ = CLOSED; |
311 } | 325 } |
312 | 326 |
313 void AudioInputController::DoReportError() { | 327 void AudioInputController::DoReportError() { |
314 DCHECK(task_runner_->BelongsToCurrentThread()); | 328 DCHECK(task_runner_->BelongsToCurrentThread()); |
315 if (handler_) | 329 if (handler_) |
316 handler_->OnError(this, STREAM_ERROR); | 330 handler_->OnError(this, STREAM_ERROR); |
317 } | 331 } |
318 | 332 |
319 void AudioInputController::DoSetVolume(double volume) { | 333 void AudioInputController::DoSetVolume(double volume) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 | 481 |
468 void AudioInputController::DoLogAudioLevel(float level_dbfs) { | 482 void AudioInputController::DoLogAudioLevel(float level_dbfs) { |
469 #if defined(AUDIO_POWER_MONITORING) | 483 #if defined(AUDIO_POWER_MONITORING) |
470 DCHECK(task_runner_->BelongsToCurrentThread()); | 484 DCHECK(task_runner_->BelongsToCurrentThread()); |
471 if (!handler_) | 485 if (!handler_) |
472 return; | 486 return; |
473 | 487 |
474 std::string log_string = base::StringPrintf( | 488 std::string log_string = base::StringPrintf( |
475 "AIC::OnData: average audio level=%.2f dBFS", level_dbfs); | 489 "AIC::OnData: average audio level=%.2f dBFS", level_dbfs); |
476 static const float kSilenceThresholdDBFS = -72.24719896f; | 490 static const float kSilenceThresholdDBFS = -72.24719896f; |
477 if (level_dbfs < kSilenceThresholdDBFS) | 491 if (level_dbfs < kSilenceThresholdDBFS) { |
478 log_string += " <=> no audio input!"; | 492 log_string += " <=> no audio input!"; |
| 493 if (!last_audio_level_was_silence_) |
| 494 ++silence_counter_; |
| 495 last_audio_level_was_silence_ = true; |
| 496 } else { |
| 497 last_audio_level_was_silence_ = false; |
| 498 } |
479 | 499 |
480 handler_->OnLog(this, log_string); | 500 handler_->OnLog(this, log_string); |
481 #endif | 501 #endif |
482 } | 502 } |
483 | 503 |
484 void AudioInputController::OnError(AudioInputStream* stream) { | 504 void AudioInputController::OnError(AudioInputStream* stream) { |
485 // Handle error on the audio-manager thread. | 505 // Handle error on the audio-manager thread. |
486 task_runner_->PostTask(FROM_HERE, base::Bind( | 506 task_runner_->PostTask(FROM_HERE, base::Bind( |
487 &AudioInputController::DoReportError, this)); | 507 &AudioInputController::DoReportError, this)); |
488 } | 508 } |
(...skipping 14 matching lines...) Expand all Loading... |
503 | 523 |
504 void AudioInputController::SetDataIsActive(bool enabled) { | 524 void AudioInputController::SetDataIsActive(bool enabled) { |
505 base::subtle::Release_Store(&data_is_active_, enabled); | 525 base::subtle::Release_Store(&data_is_active_, enabled); |
506 } | 526 } |
507 | 527 |
508 bool AudioInputController::GetDataIsActive() { | 528 bool AudioInputController::GetDataIsActive() { |
509 return (base::subtle::Acquire_Load(&data_is_active_) != false); | 529 return (base::subtle::Acquire_Load(&data_is_active_) != false); |
510 } | 530 } |
511 | 531 |
512 } // namespace media | 532 } // namespace media |
OLD | NEW |