Index: media/audio/mac/audio_low_latency_input_mac.cc |
diff --git a/media/audio/mac/audio_low_latency_input_mac.cc b/media/audio/mac/audio_low_latency_input_mac.cc |
index 22b6df5ce169f78a70de6cbec04ae1071e30c54d..2d543cafc6e567221601c5dd1e3bbd92a4489695 100644 |
--- a/media/audio/mac/audio_low_latency_input_mac.cc |
+++ b/media/audio/mac/audio_low_latency_input_mac.cc |
@@ -12,6 +12,8 @@ |
#include "media/audio/audio_util.h" |
#include "media/audio/mac/audio_manager_mac.h" |
+static const int kMinIntervalBetweenVolumeUpdatesMs = 1000; |
+ |
static std::ostream& operator<<(std::ostream& os, |
const AudioStreamBasicDescription& format) { |
os << "sample rate : " << format.mSampleRate << std::endl |
@@ -38,7 +40,9 @@ AUAudioInputStream::AUAudioInputStream( |
input_device_id_(audio_device_id), |
started_(false), |
hardware_latency_frames_(0), |
- number_of_channels_in_frame_(0) { |
+ number_of_channels_in_frame_(0), |
+ volume_(0.0), |
+ agc_is_enabled_(0) { |
DCHECK(manager_); |
// Set up the desired (output) format specified by the client. |
@@ -290,6 +294,8 @@ double AUAudioInputStream::GetMaxVolume() { |
void AUAudioInputStream::SetVolume(double volume) { |
DCHECK(volume <= 1.0 && volume >= 0.0); |
+ // TODO(henrika): remove! |
+ DLOG(INFO) << "SetVolume(volume=" << volume << ")"; |
tommi (sloooow) - chröme
2012/03/16 13:31:05
ping
henrika (OOO until Aug 14)
2012/03/21 10:16:04
Done.
|
// Verify that we have a valid device. |
if (input_device_id_ == kAudioObjectUnknown) { |
@@ -336,6 +342,16 @@ void AUAudioInputStream::SetVolume(double volume) { |
DLOG_IF(WARNING, successful_channels == 0) |
<< "Failed to set volume to " << volume_float32; |
+ |
+ // We take new volume samples once every second when the AGC is enabled. |
+ // To ensure that a new setting has an immediate effect, the new volume |
+ // setting is cached here. It will ensure that the next OnData() callback |
+ // will contain a new valid volume level. If this approach was not taken, |
+ // we could report invalid volume levels to the client for a time period |
+ // of up to one second. |
+ if (GetAutomaticGainControl()) { |
+ volume_ = GetVolume(); |
+ } |
} |
double AUAudioInputStream::GetVolume() { |
@@ -396,6 +412,14 @@ double AUAudioInputStream::GetVolume() { |
return 0.0; |
} |
+void AUAudioInputStream::SetAutomaticGainControl(bool enabled) { |
+ base::subtle::Release_Store(&agc_is_enabled_, enabled); |
+} |
+ |
+bool AUAudioInputStream::GetAutomaticGainControl() { |
+ return (base::subtle::Acquire_Load(&agc_is_enabled_) == 1); |
+} |
+ |
// AUHAL AudioDeviceOutput unit callback |
OSStatus AUAudioInputStream::InputProc(void* user_data, |
AudioUnitRenderActionFlags* flags, |
@@ -441,7 +465,25 @@ OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, |
if (!audio_data) |
return kAudioUnitErr_InvalidElement; |
- sink_->OnData(this, audio_data, buffer.mDataByteSize, capture_delay_bytes); |
+ // Query a new volume level if AGC is enabled and if it is time to update |
+ // the old value. |
+ if (GetAutomaticGainControl()) { |
+ base::Time now = base::Time::Now(); |
+ if ((now - last_volume_update_time_).InMilliseconds() > |
+ kMinIntervalBetweenVolumeUpdatesMs) { |
+ // Retrieve the current volume level. Range is [0.0,1.0]. |
+ volume_ = static_cast<double>(GetVolume()); |
+ DLOG(INFO) << "Volume updated to: " << volume_; |
scherkus (not reviewing)
2012/03/20 13:49:41
logspam
henrika (OOO until Aug 14)
2012/03/21 10:16:04
Done.
|
+ last_volume_update_time_ = now; |
+ } |
+ } |
+ |
+ // Deliver data packet, delay estimation and volume level to the user. |
+ sink_->OnData(this, |
+ audio_data, |
+ buffer.mDataByteSize, |
+ capture_delay_bytes, |
+ volume_); |
return noErr; |
} |