| 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..4dd79c77e0f3fbc4dfe921c0d64eacc402160a44 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();
|
| +
|
| return true;
|
| }
|
|
|
| @@ -263,6 +266,138 @@ void AUAudioInputStream::Close() {
|
| manager_->ReleaseInputStream(this);
|
| }
|
|
|
| +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;
|
| +}
|
| +
|
| +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.
|
| + int success_on_channel = 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_on_channel;
|
| + }
|
| + }
|
| +
|
| + DLOG_IF(WARNING, success_on_channel == 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;
|
| + 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);
|
| + }
|
| +}
|
| +
|
| // 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() {
|
| + // 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;
|
| +}
|
|
|