OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "media/audio/audio_input_stream_impl.h" |
| 7 |
| 8 static const int kMinIntervalBetweenVolumeUpdatesMs = 1000; |
| 9 |
| 10 AudioInputStreamImpl::AudioInputStreamImpl() |
| 11 : agc_is_enabled_(false), |
| 12 max_volume_(0.0), |
| 13 normalized_volume_(0.0) { |
| 14 } |
| 15 |
| 16 AudioInputStreamImpl::~AudioInputStreamImpl() {} |
| 17 |
| 18 void AudioInputStreamImpl::SetAutomaticGainControl(bool enabled) { |
| 19 agc_is_enabled_ = enabled; |
| 20 } |
| 21 |
| 22 bool AudioInputStreamImpl::GetAutomaticGainControl() { |
| 23 return agc_is_enabled_; |
| 24 } |
| 25 |
| 26 void AudioInputStreamImpl::UpdateAgcVolume() { |
| 27 base::AutoLock lock(lock_); |
| 28 |
| 29 // We take new volume samples once every second when the AGC is enabled. |
| 30 // To ensure that a new setting has an immediate effect, the new volume |
| 31 // setting is cached here. It will ensure that the next OnData() callback |
| 32 // will contain a new valid volume level. If this approach was not taken, |
| 33 // we could report invalid volume levels to the client for a time period |
| 34 // of up to one second. |
| 35 if (agc_is_enabled_) { |
| 36 GetNormalizedVolume(); |
| 37 } |
| 38 } |
| 39 |
| 40 void AudioInputStreamImpl::QueryAgcVolume(double* normalized_volume) { |
| 41 base::AutoLock lock(lock_); |
| 42 |
| 43 // Only modify the |volume| output reference if AGC is enabled and if |
| 44 // more than one second has passed since the volume was updated the last time. |
| 45 if (agc_is_enabled_) { |
| 46 base::Time now = base::Time::Now(); |
| 47 if ((now - last_volume_update_time_).InMilliseconds() > |
| 48 kMinIntervalBetweenVolumeUpdatesMs) { |
| 49 GetNormalizedVolume(); |
| 50 last_volume_update_time_ = now; |
| 51 } |
| 52 *normalized_volume = normalized_volume_; |
| 53 } |
| 54 } |
| 55 |
| 56 void AudioInputStreamImpl::GetNormalizedVolume() { |
| 57 if (max_volume_ == 0.0) { |
| 58 // Cach the maximum volume if this is the first time we ask for it. |
| 59 max_volume_ = GetMaxVolume(); |
| 60 } |
| 61 |
| 62 if (max_volume_ != 0.0) { |
| 63 // Retrieve the current volume level by asking the audio hardware. |
| 64 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux. |
| 65 normalized_volume_ = GetVolume() / max_volume_; |
| 66 } |
| 67 } |
| 68 |
OLD | NEW |