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 ec97fe9f7ed667a2a561eb8d7fe64ae36a74cbfb..506ca6d329b6b0e2cb268223cc4919b127ea2958 100644 |
--- a/media/audio/mac/audio_low_latency_input_mac.cc |
+++ b/media/audio/mac/audio_low_latency_input_mac.cc |
@@ -37,7 +37,8 @@ AUAudioInputStream::AUAudioInputStream( |
audio_unit_(0), |
input_device_id_(audio_device_id), |
started_(false), |
- hardware_latency_frames_(0) { |
+ hardware_latency_frames_(0), |
+ number_of_channels_in_frame_(0) { |
DCHECK(manager_); |
// Set up the desired (output) format specified by the client. |
@@ -211,6 +212,8 @@ bool AUAudioInputStream::Open() { |
// The hardware latency is fixed and will not change during the call. |
hardware_latency_frames_ = GetHardwareLatency(); |
+ number_of_channels_in_frame_ = GetNumberOfChannelsFromStream(); |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Why adding "FromStream()"?
no longer working on chromium
2012/02/21 18:01:38
I want to make it more specific to differentiate t
|
+ |
return true; |
} |
@@ -263,6 +266,138 @@ void AUAudioInputStream::Close() { |
manager_->ReleaseInputStream(this); |
} |
+void AUAudioInputStream::SetMicVolume(double volume) { |
+ DCHECK(volume <= 1.0 && volume >= 0.0); |
+ |
+ // Verify that we have a valid device. |
+ if (input_device_id_ == kAudioObjectUnknown) |
+ return; |
+ |
+ Float32 volume_float32 = static_cast<Float32> (volume); |
+ AudioObjectPropertyAddress property_address = { |
+ kAudioDevicePropertyVolumeScalar, |
+ kAudioDevicePropertyScopeInput, |
+ kAudioObjectPropertyElementMaster |
+ }; |
+ |
+ // Try to set the volume for master volume channel. |
+ if (IsVolumeSettableOnChannel(kAudioObjectPropertyElementMaster)) { |
+ UInt32 size = sizeof(volume_float32); |
+ OSStatus result = AudioObjectSetPropertyData(input_device_id_, |
+ &property_address, |
+ 0, |
+ NULL, |
+ size, |
+ &volume_float32); |
+ DLOG_IF(WARNING, result != noErr) << "SetMicVolume failed to set volume to " |
+ << volume_float32; |
+ return; |
+ } |
+ |
+ // There is no master volume control, try to set volume for each channel. |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Can there be one master volume and channel volumes
no longer working on chromium
2012/02/21 18:01:38
If it supports master volume, we use master volume
|
+ int success = 0; |
+ for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
+ property_address.mElement = i; |
+ if (IsVolumeSettableOnChannel(i)) { |
+ UInt32 size = sizeof(volume_float32); |
+ OSStatus result = AudioObjectSetPropertyData(input_device_id_, |
+ &property_address, |
+ 0, |
+ NULL, |
+ size, |
+ &volume_float32); |
+ if (result == noErr) |
+ ++success; |
+ } |
+ } |
+ |
+ DLOG_IF(WARNING, success == noErr) << "SetMicVolume failed to set volume to " |
+ << volume_float32; |
+} |
+ |
+void AUAudioInputStream::GetMicVolume(double* volume) { |
+ // Verify that we have a valid device. |
+ if (input_device_id_ == kAudioObjectUnknown) |
+ return; |
+ |
+ *volume = 0.0; |
+ AudioObjectPropertyAddress property_address = { |
+ kAudioDevicePropertyVolumeScalar, |
+ kAudioDevicePropertyScopeInput, |
+ kAudioObjectPropertyElementMaster |
+ }; |
+ |
+ if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
+ // The device supports master volume control, get the volume from the |
+ // master channel. |
+ Float32 volume_float32 = 0.0; |
+ UInt32 size = sizeof(volume_float32); |
+ OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
+ &property_address, |
+ 0, |
+ NULL, |
+ &size, |
+ &volume_float32); |
+ if (result != noErr) { |
+ DLOG(WARNING) << "AudioObjectGetPropertyData failed not get volume."; |
+ return; |
+ } |
+ *volume = static_cast<double> (volume_float32); |
+ } else { |
+ // There is no master volume control, try to get the average volume of |
+ // all the channels. |
+ Float32 volume_float32 = 0.0; |
+ int success = 0; |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Not clear that this is a counter.
no longer working on chromium
2012/02/21 18:01:38
Done.
|
+ for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
+ property_address.mElement = i; |
+ if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
+ Float32 channel_volume = 0; |
+ UInt32 size = sizeof(channel_volume); |
+ OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
+ &property_address, |
+ 0, |
+ NULL, |
+ &size, |
+ &channel_volume); |
+ if (result == noErr) { |
+ volume_float32 += channel_volume; |
+ ++success; |
+ } |
+ } |
+ } |
+ if (success == 0) { |
+ DLOG(WARNING) << "Unable to get volume from any channel"; |
+ return; |
+ } |
+ |
+ // Get the average volume of the channels. |
+ *volume = static_cast<double> (volume_float32 / success); |
+ } |
+} |
+ |
+void AUAudioInputStream::GetMaxMicVolume(double* volume) { |
+ // Verify that we have a valid device. |
+ if (input_device_id_ == kAudioObjectUnknown) |
+ return; |
+ |
+ if (IsVolumeSettableOnChannel(0)) { |
+ // If the volume is settable, the valid volume range is [0.0, 1.0]. |
+ *volume = 1.0; |
+ return; |
+ } |
+ |
+ // There is no master volume control, try to set volume for each channel. |
+ for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
+ if (IsVolumeSettableOnChannel(i)) { |
+ *volume = 1.0; |
+ return; |
+ } |
+ } |
+ |
+ // Volume control is not available for the audio stream. |
+ *volume = 0.0; |
+} |
+ |
// AUHAL AudioDeviceOutput unit callback |
OSStatus AUAudioInputStream::InputProc(void* user_data, |
AudioUnitRenderActionFlags* flags, |
@@ -438,9 +573,43 @@ double AUAudioInputStream::GetCaptureLatency( |
return (delay_frames + hardware_latency_frames_); |
} |
+UInt32 AUAudioInputStream::GetNumberOfChannelsFromStream() { |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Why not the same APIs as in the audio manager for
no longer working on chromium
2012/02/21 18:01:38
I explored a bit on the apple documentation. It se
|
+ // Get the stream format, to be able to read the number of channels. |
+ AudioObjectPropertyAddress property_address = { |
+ kAudioDevicePropertyStreamFormat, |
+ kAudioDevicePropertyScopeInput, |
+ kAudioObjectPropertyElementMaster |
+ }; |
+ AudioStreamBasicDescription stream_format; |
+ UInt32 size = sizeof(stream_format); |
+ OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
+ &property_address, |
+ 0, |
+ NULL, |
+ &size, |
+ &stream_format); |
+ DLOG_IF(WARNING, result != noErr) << "Could not get stream format."; |
+ |
+ return stream_format.mChannelsPerFrame; |
+} |
+ |
void AUAudioInputStream::HandleError(OSStatus err) { |
NOTREACHED() << "error " << GetMacOSStatusErrorString(err) |
<< " (" << err << ")"; |
if (sink_) |
sink_->OnError(this, static_cast<int>(err)); |
} |
+ |
+bool AUAudioInputStream::IsVolumeSettableOnChannel(int channel) { |
+ Boolean is_settable = false; |
+ AudioObjectPropertyAddress property_address = { |
+ kAudioDevicePropertyVolumeScalar, |
+ kAudioDevicePropertyScopeInput, |
+ kAudioObjectPropertyElementMaster |
+ }; |
+ property_address.mElement = channel; |
+ OSStatus result = AudioObjectIsPropertySettable(input_device_id_, |
+ &property_address, |
+ &is_settable); |
+ return (result == noErr)? is_settable : false; |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Same nit again. Please use ") ? is_"
no longer working on chromium
2012/02/21 18:01:38
Done.
|
+} |