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