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 "media/audio/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 23 #include "media/base/user_input_monitor.h" | 23 #include "media/base/user_input_monitor.h" |
| 24 | 24 |
| 25 namespace media { | 25 namespace media { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const int kMaxInputChannels = 3; | 28 const int kMaxInputChannels = 3; |
| 29 constexpr int kCheckMutedStateIntervalSeconds = 1; | |
| 29 | 30 |
| 30 #if defined(AUDIO_POWER_MONITORING) | 31 #if defined(AUDIO_POWER_MONITORING) |
| 31 // Time in seconds between two successive measurements of audio power levels. | 32 // Time in seconds between two successive measurements of audio power levels. |
| 32 const int kPowerMonitorLogIntervalSeconds = 15; | 33 const int kPowerMonitorLogIntervalSeconds = 15; |
| 33 | 34 |
| 34 // A warning will be logged when the microphone audio volume is below this | 35 // A warning will be logged when the microphone audio volume is below this |
| 35 // threshold. | 36 // threshold. |
| 36 const int kLowLevelMicrophoneLevelPercent = 10; | 37 const int kLowLevelMicrophoneLevelPercent = 10; |
| 37 | 38 |
| 38 // Logs if the user has enabled the microphone mute or not. This is normally | 39 // Logs if the user has enabled the microphone mute or not. This is normally |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 #if BUILDFLAG(ENABLE_WEBRTC) | 191 #if BUILDFLAG(ENABLE_WEBRTC) |
| 191 debug_recording_helper_(params, task_runner_, base::OnceClosure()), | 192 debug_recording_helper_(params, task_runner_, base::OnceClosure()), |
| 192 #endif | 193 #endif |
| 193 weak_ptr_factory_(this) { | 194 weak_ptr_factory_(this) { |
| 194 DCHECK(creator_task_runner_.get()); | 195 DCHECK(creator_task_runner_.get()); |
| 195 DCHECK(handler_); | 196 DCHECK(handler_); |
| 196 DCHECK(sync_writer_); | 197 DCHECK(sync_writer_); |
| 197 } | 198 } |
| 198 | 199 |
| 199 AudioInputController::~AudioInputController() { | 200 AudioInputController::~AudioInputController() { |
| 200 DCHECK(!audio_callback_); | 201 DCHECK(!audio_callback_); |
|
Guido Urdaneta
2017/06/02 14:13:21
DCHECK that the timer has no value?
ossu-chromium
2017/06/02 15:32:45
Done.
| |
| 201 DCHECK(!stream_); | 202 DCHECK(!stream_); |
| 202 } | 203 } |
| 203 | 204 |
| 204 // static | 205 // static |
| 205 scoped_refptr<AudioInputController> AudioInputController::Create( | 206 scoped_refptr<AudioInputController> AudioInputController::Create( |
| 206 AudioManager* audio_manager, | 207 AudioManager* audio_manager, |
| 207 EventHandler* event_handler, | 208 EventHandler* event_handler, |
| 208 SyncWriter* sync_writer, | 209 SyncWriter* sync_writer, |
| 209 UserInputMonitor* user_input_monitor, | 210 UserInputMonitor* user_input_monitor, |
| 210 const AudioParameters& params, | 211 const AudioParameters& params, |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 // functionality to modify the input volume slider. One such example is | 347 // functionality to modify the input volume slider. One such example is |
| 347 // Windows XP. | 348 // Windows XP. |
| 348 power_measurement_is_enabled_ &= agc_is_supported; | 349 power_measurement_is_enabled_ &= agc_is_supported; |
| 349 #else | 350 #else |
| 350 stream_to_control->SetAutomaticGainControl(enable_agc); | 351 stream_to_control->SetAutomaticGainControl(enable_agc); |
| 351 #endif | 352 #endif |
| 352 | 353 |
| 353 // Finally, keep the stream pointer around, update the state and notify. | 354 // Finally, keep the stream pointer around, update the state and notify. |
| 354 stream_ = stream_to_control; | 355 stream_ = stream_to_control; |
| 355 handler_->OnCreated(this); | 356 handler_->OnCreated(this); |
| 357 | |
| 358 // Check the current muted state and create a new repeating timer to keep that | |
| 359 // updated. | |
| 360 CheckMutedState(); | |
| 361 check_muted_state_timer_.emplace(); | |
| 362 check_muted_state_timer_->Start( | |
| 363 FROM_HERE, base::TimeDelta::FromSeconds(kCheckMutedStateIntervalSeconds), | |
| 364 this, &AudioInputController::CheckMutedState); | |
| 365 DCHECK(check_muted_state_timer_->IsRunning()); | |
| 356 } | 366 } |
| 357 | 367 |
| 358 void AudioInputController::DoRecord() { | 368 void AudioInputController::DoRecord() { |
| 359 DCHECK(task_runner_->BelongsToCurrentThread()); | 369 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 360 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); | 370 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); |
| 361 | 371 |
| 362 if (!stream_ || audio_callback_) | 372 if (!stream_ || audio_callback_) |
| 363 return; | 373 return; |
| 364 | 374 |
| 365 handler_->OnLog(this, "AIC::DoRecord"); | 375 handler_->OnLog(this, "AIC::DoRecord"); |
| 366 | 376 |
| 367 if (user_input_monitor_) { | 377 if (user_input_monitor_) { |
| 368 user_input_monitor_->EnableKeyPressMonitoring(); | 378 user_input_monitor_->EnableKeyPressMonitoring(); |
| 369 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); | 379 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); |
| 370 } | 380 } |
| 371 | 381 |
| 372 stream_create_time_ = base::TimeTicks::Now(); | 382 stream_create_time_ = base::TimeTicks::Now(); |
| 373 | 383 |
| 374 audio_callback_.reset(new AudioCallback(this)); | 384 audio_callback_.reset(new AudioCallback(this)); |
| 375 stream_->Start(audio_callback_.get()); | 385 stream_->Start(audio_callback_.get()); |
| 376 } | 386 } |
| 377 | 387 |
| 378 void AudioInputController::DoClose() { | 388 void AudioInputController::DoClose() { |
| 379 DCHECK(task_runner_->BelongsToCurrentThread()); | 389 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 380 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); | 390 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); |
| 381 | 391 |
| 382 if (!stream_) | 392 if (!stream_) |
| 383 return; | 393 return; |
| 384 | 394 |
| 395 // Stop and destroy the muted check timer. | |
| 396 check_muted_state_timer_.reset(); | |
| 397 | |
| 385 // Allow calling unconditionally and bail if we don't have a stream to close. | 398 // Allow calling unconditionally and bail if we don't have a stream to close. |
| 386 if (audio_callback_) { | 399 if (audio_callback_) { |
| 387 stream_->Stop(); | 400 stream_->Stop(); |
| 388 | 401 |
| 389 // Sometimes a stream (and accompanying audio track) is created and | 402 // Sometimes a stream (and accompanying audio track) is created and |
| 390 // immediately closed or discarded. In this case they are registered as | 403 // immediately closed or discarded. In this case they are registered as |
| 391 // 'stopped early' rather than 'never got data'. | 404 // 'stopped early' rather than 'never got data'. |
| 392 const base::TimeDelta duration = | 405 const base::TimeDelta duration = |
| 393 base::TimeTicks::Now() - stream_create_time_; | 406 base::TimeTicks::Now() - stream_create_time_; |
| 394 CaptureStartupResult capture_startup_result = | 407 CaptureStartupResult capture_startup_result = |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 *mic_volume_percent = static_cast<int>(100.0 * volume); | 658 *mic_volume_percent = static_cast<int>(100.0 * volume); |
| 646 | 659 |
| 647 last_audio_level_log_time_ = now; | 660 last_audio_level_log_time_ = now; |
| 648 | 661 |
| 649 return true; | 662 return true; |
| 650 #else | 663 #else |
| 651 return false; | 664 return false; |
| 652 #endif | 665 #endif |
| 653 } | 666 } |
| 654 | 667 |
| 668 void AudioInputController::CheckMutedState() { | |
| 669 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 670 DCHECK(stream_); | |
| 671 const bool new_state = stream_->IsMuted(); | |
| 672 if (new_state != is_muted_) { | |
| 673 is_muted_ = new_state; | |
| 674 // Don't log this here, AudioInputRendererHost will log instead. | |
|
Guido Urdaneta
2017/06/02 14:13:21
remove obsolete comment
ossu-chromium
2017/06/02 15:32:45
No, I added it so that anyone scouring this code,
| |
| 675 handler_->OnMuted(this, is_muted_); | |
| 676 } | |
| 677 } | |
| 678 | |
| 655 // static | 679 // static |
| 656 AudioInputController::StreamType AudioInputController::ParamsToStreamType( | 680 AudioInputController::StreamType AudioInputController::ParamsToStreamType( |
| 657 const AudioParameters& params) { | 681 const AudioParameters& params) { |
| 658 switch (params.format()) { | 682 switch (params.format()) { |
| 659 case AudioParameters::Format::AUDIO_PCM_LINEAR: | 683 case AudioParameters::Format::AUDIO_PCM_LINEAR: |
| 660 return AudioInputController::StreamType::HIGH_LATENCY; | 684 return AudioInputController::StreamType::HIGH_LATENCY; |
| 661 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: | 685 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: |
| 662 return AudioInputController::StreamType::LOW_LATENCY; | 686 return AudioInputController::StreamType::LOW_LATENCY; |
| 663 default: | 687 default: |
| 664 // Currently, the remaining supported type is fake. Reconsider if other | 688 // Currently, the remaining supported type is fake. Reconsider if other |
| 665 // formats become supported. | 689 // formats become supported. |
| 666 return AudioInputController::StreamType::FAKE; | 690 return AudioInputController::StreamType::FAKE; |
| 667 } | 691 } |
| 668 } | 692 } |
| 669 | 693 |
| 670 } // namespace media | 694 } // namespace media |
| OLD | NEW |