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