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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_); |
201 DCHECK(!stream_); | 202 DCHECK(!stream_); |
| 203 DCHECK(!check_muted_state_timer_.IsRunning()); |
202 } | 204 } |
203 | 205 |
204 // static | 206 // static |
205 scoped_refptr<AudioInputController> AudioInputController::Create( | 207 scoped_refptr<AudioInputController> AudioInputController::Create( |
206 AudioManager* audio_manager, | 208 AudioManager* audio_manager, |
207 EventHandler* event_handler, | 209 EventHandler* event_handler, |
208 SyncWriter* sync_writer, | 210 SyncWriter* sync_writer, |
209 UserInputMonitor* user_input_monitor, | 211 UserInputMonitor* user_input_monitor, |
210 const AudioParameters& params, | 212 const AudioParameters& params, |
211 const std::string& device_id, | 213 const std::string& device_id, |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 // functionality to modify the input volume slider. One such example is | 353 // functionality to modify the input volume slider. One such example is |
352 // Windows XP. | 354 // Windows XP. |
353 power_measurement_is_enabled_ &= agc_is_supported; | 355 power_measurement_is_enabled_ &= agc_is_supported; |
354 #else | 356 #else |
355 stream_to_control->SetAutomaticGainControl(enable_agc); | 357 stream_to_control->SetAutomaticGainControl(enable_agc); |
356 #endif | 358 #endif |
357 | 359 |
358 // Finally, keep the stream pointer around, update the state and notify. | 360 // Finally, keep the stream pointer around, update the state and notify. |
359 stream_ = stream_to_control; | 361 stream_ = stream_to_control; |
360 handler_->OnCreated(this); | 362 handler_->OnCreated(this); |
| 363 |
| 364 // Check the current muted state and start the repeating timer to keep that |
| 365 // updated. |
| 366 CheckMutedState(); |
| 367 check_muted_state_timer_.Start( |
| 368 FROM_HERE, base::TimeDelta::FromSeconds(kCheckMutedStateIntervalSeconds), |
| 369 this, &AudioInputController::CheckMutedState); |
| 370 DCHECK(check_muted_state_timer_.IsRunning()); |
361 } | 371 } |
362 | 372 |
363 void AudioInputController::DoRecord() { | 373 void AudioInputController::DoRecord() { |
364 DCHECK(task_runner_->BelongsToCurrentThread()); | 374 DCHECK(task_runner_->BelongsToCurrentThread()); |
365 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); | 375 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); |
366 | 376 |
367 if (!stream_ || audio_callback_) | 377 if (!stream_ || audio_callback_) |
368 return; | 378 return; |
369 | 379 |
370 handler_->OnLog(this, "AIC::DoRecord"); | 380 handler_->OnLog(this, "AIC::DoRecord"); |
371 | 381 |
372 if (user_input_monitor_) { | 382 if (user_input_monitor_) { |
373 user_input_monitor_->EnableKeyPressMonitoring(); | 383 user_input_monitor_->EnableKeyPressMonitoring(); |
374 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); | 384 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); |
375 } | 385 } |
376 | 386 |
377 stream_create_time_ = base::TimeTicks::Now(); | 387 stream_create_time_ = base::TimeTicks::Now(); |
378 | 388 |
379 audio_callback_.reset(new AudioCallback(this)); | 389 audio_callback_.reset(new AudioCallback(this)); |
380 stream_->Start(audio_callback_.get()); | 390 stream_->Start(audio_callback_.get()); |
381 } | 391 } |
382 | 392 |
383 void AudioInputController::DoClose() { | 393 void AudioInputController::DoClose() { |
384 DCHECK(task_runner_->BelongsToCurrentThread()); | 394 DCHECK(task_runner_->BelongsToCurrentThread()); |
385 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); | 395 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); |
386 | 396 |
387 if (!stream_) | 397 if (!stream_) |
388 return; | 398 return; |
389 | 399 |
| 400 check_muted_state_timer_.Stop(); |
| 401 |
390 std::string log_string; | 402 std::string log_string; |
391 static const char kLogStringPrefix[] = "AIC::DoClose:"; | 403 static const char kLogStringPrefix[] = "AIC::DoClose:"; |
392 | 404 |
| 405 // Allow calling unconditionally and bail if we don't have a stream to close. |
393 if (audio_callback_) { | 406 if (audio_callback_) { |
394 stream_->Stop(); | 407 stream_->Stop(); |
395 | 408 |
396 // Sometimes a stream (and accompanying audio track) is created and | 409 // Sometimes a stream (and accompanying audio track) is created and |
397 // immediately closed or discarded. In this case they are registered as | 410 // immediately closed or discarded. In this case they are registered as |
398 // 'stopped early' rather than 'never got data'. | 411 // 'stopped early' rather than 'never got data'. |
399 const base::TimeDelta duration = | 412 const base::TimeDelta duration = |
400 base::TimeTicks::Now() - stream_create_time_; | 413 base::TimeTicks::Now() - stream_create_time_; |
401 CaptureStartupResult capture_startup_result = | 414 CaptureStartupResult capture_startup_result = |
402 audio_callback_->received_callback() | 415 audio_callback_->received_callback() |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 *mic_volume_percent = static_cast<int>(100.0 * volume); | 671 *mic_volume_percent = static_cast<int>(100.0 * volume); |
659 | 672 |
660 last_audio_level_log_time_ = now; | 673 last_audio_level_log_time_ = now; |
661 | 674 |
662 return true; | 675 return true; |
663 #else | 676 #else |
664 return false; | 677 return false; |
665 #endif | 678 #endif |
666 } | 679 } |
667 | 680 |
| 681 void AudioInputController::CheckMutedState() { |
| 682 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 683 DCHECK(stream_); |
| 684 const bool new_state = stream_->IsMuted(); |
| 685 if (new_state != is_muted_) { |
| 686 is_muted_ = new_state; |
| 687 // We don't log OnMuted here, but leave that for AudioInputRendererHost. |
| 688 handler_->OnMuted(this, is_muted_); |
| 689 } |
| 690 } |
| 691 |
668 // static | 692 // static |
669 AudioInputController::StreamType AudioInputController::ParamsToStreamType( | 693 AudioInputController::StreamType AudioInputController::ParamsToStreamType( |
670 const AudioParameters& params) { | 694 const AudioParameters& params) { |
671 switch (params.format()) { | 695 switch (params.format()) { |
672 case AudioParameters::Format::AUDIO_PCM_LINEAR: | 696 case AudioParameters::Format::AUDIO_PCM_LINEAR: |
673 return AudioInputController::StreamType::HIGH_LATENCY; | 697 return AudioInputController::StreamType::HIGH_LATENCY; |
674 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: | 698 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: |
675 return AudioInputController::StreamType::LOW_LATENCY; | 699 return AudioInputController::StreamType::LOW_LATENCY; |
676 default: | 700 default: |
677 // Currently, the remaining supported type is fake. Reconsider if other | 701 // Currently, the remaining supported type is fake. Reconsider if other |
678 // formats become supported. | 702 // formats become supported. |
679 return AudioInputController::StreamType::FAKE; | 703 return AudioInputController::StreamType::FAKE; |
680 } | 704 } |
681 } | 705 } |
682 | 706 |
683 } // namespace media | 707 } // namespace media |
OLD | NEW |