| 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..66423067d378691e1632a8115f5b83c4a12f9e23 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.
|
| @@ -289,7 +293,8 @@ double AUAudioInputStream::GetMaxVolume() {
|
| }
|
|
|
| void AUAudioInputStream::SetVolume(double volume) {
|
| - DCHECK(volume <= 1.0 && volume >= 0.0);
|
| + DCHECK_GE(volume, 0.0);
|
| + DCHECK_LE(volume, 1.0);
|
|
|
| // Verify that we have a valid device.
|
| if (input_device_id_ == kAudioObjectUnknown) {
|
| @@ -336,6 +341,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 +411,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 +464,24 @@ 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());
|
| + 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;
|
| }
|
|
|