| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/mac/audio_low_latency_input_mac.h" | 5 #include "media/audio/mac/audio_low_latency_input_mac.h" |
| 6 | 6 |
| 7 #include <CoreServices/CoreServices.h> | 7 #include <CoreServices/CoreServices.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/mac_logging.h" | 11 #include "base/mac/mac_logging.h" |
| 12 #include "media/audio/mac/audio_manager_mac.h" | 12 #include "media/audio/mac/audio_manager_mac.h" |
| 13 #include "media/base/audio_block_fifo.h" | |
| 14 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 15 #include "media/base/data_buffer.h" | 14 #include "media/base/data_buffer.h" |
| 16 | 15 |
| 17 namespace media { | 16 namespace media { |
| 18 | 17 |
| 19 // Number of blocks of buffers used in the |fifo_|. | 18 // Number of blocks of buffers used in the |fifo_|. |
| 20 const int kNumberOfBlocksBufferInFifo = 2; | 19 const int kNumberOfBlocksBufferInFifo = 2; |
| 21 | 20 |
| 22 static std::ostream& operator<<(std::ostream& os, | 21 static std::ostream& operator<<(std::ostream& os, |
| 23 const AudioStreamBasicDescription& format) { | 22 const AudioStreamBasicDescription& format) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 const AudioParameters& input_params, | 39 const AudioParameters& input_params, |
| 41 AudioDeviceID audio_device_id) | 40 AudioDeviceID audio_device_id) |
| 42 : manager_(manager), | 41 : manager_(manager), |
| 43 number_of_frames_(input_params.frames_per_buffer()), | 42 number_of_frames_(input_params.frames_per_buffer()), |
| 44 sink_(NULL), | 43 sink_(NULL), |
| 45 audio_unit_(0), | 44 audio_unit_(0), |
| 46 input_device_id_(audio_device_id), | 45 input_device_id_(audio_device_id), |
| 47 started_(false), | 46 started_(false), |
| 48 hardware_latency_frames_(0), | 47 hardware_latency_frames_(0), |
| 49 number_of_channels_in_frame_(0), | 48 number_of_channels_in_frame_(0), |
| 50 output_bus_(AudioBus::Create(input_params)) { | 49 fifo_(input_params.channels(), |
| 50 number_of_frames_, |
| 51 kNumberOfBlocksBufferInFifo) { |
| 51 DCHECK(manager_); | 52 DCHECK(manager_); |
| 52 | 53 |
| 53 // Set up the desired (output) format specified by the client. | 54 // Set up the desired (output) format specified by the client. |
| 54 format_.mSampleRate = input_params.sample_rate(); | 55 format_.mSampleRate = input_params.sample_rate(); |
| 55 format_.mFormatID = kAudioFormatLinearPCM; | 56 format_.mFormatID = kAudioFormatLinearPCM; |
| 56 format_.mFormatFlags = | 57 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
| 57 kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved; | 58 kLinearPCMFormatFlagIsSignedInteger; |
| 58 size_t bytes_per_sample = sizeof(Float32); | 59 format_.mBitsPerChannel = input_params.bits_per_sample(); |
| 59 format_.mBitsPerChannel = bytes_per_sample * 8; | |
| 60 format_.mChannelsPerFrame = input_params.channels(); | 60 format_.mChannelsPerFrame = input_params.channels(); |
| 61 format_.mFramesPerPacket = 1; | 61 format_.mFramesPerPacket = 1; // uncompressed audio |
| 62 format_.mBytesPerFrame = bytes_per_sample; | 62 format_.mBytesPerPacket = (format_.mBitsPerChannel * |
| 63 format_.mBytesPerPacket = format_.mBytesPerFrame * format_.mFramesPerPacket; | 63 input_params.channels()) / 8; |
| 64 format_.mBytesPerFrame = format_.mBytesPerPacket; |
| 64 format_.mReserved = 0; | 65 format_.mReserved = 0; |
| 65 | 66 |
| 66 DVLOG(1) << "Desired ouput format: " << format_; | 67 DVLOG(1) << "Desired ouput format: " << format_; |
| 67 | 68 |
| 68 // Allocate AudioBufferList based on the number of channels. | 69 // Derive size (in bytes) of the buffers that we will render to. |
| 69 audio_buffer_list_.reset(static_cast<AudioBufferList*>( | 70 UInt32 data_byte_size = number_of_frames_ * format_.mBytesPerFrame; |
| 70 malloc(sizeof(UInt32) + input_params.channels() * sizeof(AudioBuffer)))); | 71 DVLOG(1) << "Size of data buffer in bytes : " << data_byte_size; |
| 71 audio_buffer_list_->mNumberBuffers = input_params.channels(); | |
| 72 | 72 |
| 73 // Allocate AudioBuffers to be used as storage for the received audio. | 73 // Allocate AudioBuffers to be used as storage for the received audio. |
| 74 // The AudioBufferList structure works as a placeholder for the | 74 // The AudioBufferList structure works as a placeholder for the |
| 75 // AudioBuffer structure, which holds a pointer to the actual data buffer. | 75 // AudioBuffer structure, which holds a pointer to the actual data buffer. |
| 76 UInt32 data_byte_size = number_of_frames_ * format_.mBytesPerFrame; | 76 audio_data_buffer_.reset(new uint8[data_byte_size]); |
| 77 CHECK_LE(static_cast<int>(data_byte_size * input_params.channels()), | 77 audio_buffer_list_.mNumberBuffers = 1; |
| 78 media::AudioBus::CalculateMemorySize(input_params)); | 78 |
| 79 AudioBuffer* audio_buffer = audio_buffer_list_->mBuffers; | 79 AudioBuffer* audio_buffer = audio_buffer_list_.mBuffers; |
| 80 for (UInt32 i = 0; i < audio_buffer_list_->mNumberBuffers; ++i) { | 80 audio_buffer->mNumberChannels = input_params.channels(); |
| 81 audio_buffer[i].mNumberChannels = 1; | 81 audio_buffer->mDataByteSize = data_byte_size; |
| 82 audio_buffer[i].mDataByteSize = data_byte_size; | 82 audio_buffer->mData = audio_data_buffer_.get(); |
| 83 audio_buffer[i].mData = output_bus_->channel(i); | |
| 84 } | |
| 85 } | 83 } |
| 86 | 84 |
| 87 AUAudioInputStream::~AUAudioInputStream() { | 85 AUAudioInputStream::~AUAudioInputStream() {} |
| 88 } | |
| 89 | 86 |
| 90 // Obtain and open the AUHAL AudioOutputUnit for recording. | 87 // Obtain and open the AUHAL AudioOutputUnit for recording. |
| 91 bool AUAudioInputStream::Open() { | 88 bool AUAudioInputStream::Open() { |
| 92 // Verify that we are not already opened. | 89 // Verify that we are not already opened. |
| 93 if (audio_unit_) | 90 if (audio_unit_) |
| 94 return false; | 91 return false; |
| 95 | 92 |
| 96 // Verify that we have a valid device. | 93 // Verify that we have a valid device. |
| 97 if (input_device_id_ == kAudioObjectUnknown) { | 94 if (input_device_id_ == kAudioObjectUnknown) { |
| 98 NOTREACHED() << "Device ID is unknown"; | 95 NOTREACHED() << "Device ID is unknown"; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 // Note that, devices can only be set to the AUHAL after enabling IO. | 156 // Note that, devices can only be set to the AUHAL after enabling IO. |
| 160 result = AudioUnitSetProperty(audio_unit_, | 157 result = AudioUnitSetProperty(audio_unit_, |
| 161 kAudioOutputUnitProperty_CurrentDevice, | 158 kAudioOutputUnitProperty_CurrentDevice, |
| 162 kAudioUnitScope_Global, | 159 kAudioUnitScope_Global, |
| 163 0, | 160 0, |
| 164 &input_device_id_, | 161 &input_device_id_, |
| 165 sizeof(input_device_id_)); | 162 sizeof(input_device_id_)); |
| 166 if (result) { | 163 if (result) { |
| 167 HandleError(result); | 164 HandleError(result); |
| 168 return false; | 165 return false; |
| 166 } |
| 167 |
| 168 // Register the input procedure for the AUHAL. |
| 169 // This procedure will be called when the AUHAL has received new data |
| 170 // from the input device. |
| 171 AURenderCallbackStruct callback; |
| 172 callback.inputProc = InputProc; |
| 173 callback.inputProcRefCon = this; |
| 174 result = AudioUnitSetProperty(audio_unit_, |
| 175 kAudioOutputUnitProperty_SetInputCallback, |
| 176 kAudioUnitScope_Global, |
| 177 0, |
| 178 &callback, |
| 179 sizeof(callback)); |
| 180 if (result) { |
| 181 HandleError(result); |
| 182 return false; |
| 169 } | 183 } |
| 170 | 184 |
| 171 // Set up the the desired (output) format. | 185 // Set up the the desired (output) format. |
| 172 // For obtaining input from a device, the device format is always expressed | 186 // For obtaining input from a device, the device format is always expressed |
| 173 // on the output scope of the AUHAL's Element 1. | 187 // on the output scope of the AUHAL's Element 1. |
| 174 result = AudioUnitSetProperty(audio_unit_, | 188 result = AudioUnitSetProperty(audio_unit_, |
| 175 kAudioUnitProperty_StreamFormat, | 189 kAudioUnitProperty_StreamFormat, |
| 176 kAudioUnitScope_Output, | 190 kAudioUnitScope_Output, |
| 177 1, | 191 1, |
| 178 &format_, | 192 &format_, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 208 kAudioUnitScope_Output, | 222 kAudioUnitScope_Output, |
| 209 1, | 223 1, |
| 210 &buffer_size, | 224 &buffer_size, |
| 211 sizeof(buffer_size)); | 225 sizeof(buffer_size)); |
| 212 if (result != noErr) { | 226 if (result != noErr) { |
| 213 HandleError(result); | 227 HandleError(result); |
| 214 return false; | 228 return false; |
| 215 } | 229 } |
| 216 } | 230 } |
| 217 | 231 |
| 218 // Register the input procedure for the AUHAL. | |
| 219 // This procedure will be called when the AUHAL has received new data | |
| 220 // from the input device. | |
| 221 AURenderCallbackStruct callback; | |
| 222 callback.inputProc = InputProc; | |
| 223 callback.inputProcRefCon = this; | |
| 224 result = AudioUnitSetProperty(audio_unit_, | |
| 225 kAudioOutputUnitProperty_SetInputCallback, | |
| 226 kAudioUnitScope_Global, | |
| 227 0, | |
| 228 &callback, | |
| 229 sizeof(callback)); | |
| 230 if (result) { | |
| 231 HandleError(result); | |
| 232 return false; | |
| 233 } | |
| 234 | |
| 235 // Finally, initialize the audio unit and ensure that it is ready to render. | 232 // Finally, initialize the audio unit and ensure that it is ready to render. |
| 236 // Allocates memory according to the maximum number of audio frames | 233 // Allocates memory according to the maximum number of audio frames |
| 237 // it can produce in response to a single render call. | 234 // it can produce in response to a single render call. |
| 238 result = AudioUnitInitialize(audio_unit_); | 235 result = AudioUnitInitialize(audio_unit_); |
| 239 if (result) { | 236 if (result) { |
| 240 HandleError(result); | 237 HandleError(result); |
| 241 return false; | 238 return false; |
| 242 } | 239 } |
| 243 | 240 |
| 244 // The hardware latency is fixed and will not change during the call. | 241 // The hardware latency is fixed and will not change during the call. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 DCHECK_LE(volume, 1.0); | 335 DCHECK_LE(volume, 1.0); |
| 339 | 336 |
| 340 // Verify that we have a valid device. | 337 // Verify that we have a valid device. |
| 341 if (input_device_id_ == kAudioObjectUnknown) { | 338 if (input_device_id_ == kAudioObjectUnknown) { |
| 342 NOTREACHED() << "Device ID is unknown"; | 339 NOTREACHED() << "Device ID is unknown"; |
| 343 return; | 340 return; |
| 344 } | 341 } |
| 345 | 342 |
| 346 Float32 volume_float32 = static_cast<Float32>(volume); | 343 Float32 volume_float32 = static_cast<Float32>(volume); |
| 347 AudioObjectPropertyAddress property_address = { | 344 AudioObjectPropertyAddress property_address = { |
| 348 kAudioDevicePropertyVolumeScalar, | 345 kAudioDevicePropertyVolumeScalar, |
| 349 kAudioDevicePropertyScopeInput, | 346 kAudioDevicePropertyScopeInput, |
| 350 kAudioObjectPropertyElementMaster | 347 kAudioObjectPropertyElementMaster |
| 351 }; | 348 }; |
| 352 | 349 |
| 353 // Try to set the volume for master volume channel. | 350 // Try to set the volume for master volume channel. |
| 354 if (IsVolumeSettableOnChannel(kAudioObjectPropertyElementMaster)) { | 351 if (IsVolumeSettableOnChannel(kAudioObjectPropertyElementMaster)) { |
| 355 OSStatus result = AudioObjectSetPropertyData(input_device_id_, | 352 OSStatus result = AudioObjectSetPropertyData(input_device_id_, |
| 356 &property_address, | 353 &property_address, |
| 357 0, | 354 0, |
| 358 NULL, | 355 NULL, |
| 359 sizeof(volume_float32), | 356 sizeof(volume_float32), |
| 360 &volume_float32); | 357 &volume_float32); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 386 // Update the AGC volume level based on the last setting above. Note that, | 383 // Update the AGC volume level based on the last setting above. Note that, |
| 387 // the volume-level resolution is not infinite and it is therefore not | 384 // the volume-level resolution is not infinite and it is therefore not |
| 388 // possible to assume that the volume provided as input parameter can be | 385 // possible to assume that the volume provided as input parameter can be |
| 389 // used directly. Instead, a new query to the audio hardware is required. | 386 // used directly. Instead, a new query to the audio hardware is required. |
| 390 // This method does nothing if AGC is disabled. | 387 // This method does nothing if AGC is disabled. |
| 391 UpdateAgcVolume(); | 388 UpdateAgcVolume(); |
| 392 } | 389 } |
| 393 | 390 |
| 394 double AUAudioInputStream::GetVolume() { | 391 double AUAudioInputStream::GetVolume() { |
| 395 // Verify that we have a valid device. | 392 // Verify that we have a valid device. |
| 396 if (input_device_id_ == kAudioObjectUnknown) { | 393 if (input_device_id_ == kAudioObjectUnknown){ |
| 397 NOTREACHED() << "Device ID is unknown"; | 394 NOTREACHED() << "Device ID is unknown"; |
| 398 return 0.0; | 395 return 0.0; |
| 399 } | 396 } |
| 400 | 397 |
| 401 AudioObjectPropertyAddress property_address = { | 398 AudioObjectPropertyAddress property_address = { |
| 402 kAudioDevicePropertyVolumeScalar, | 399 kAudioDevicePropertyVolumeScalar, |
| 403 kAudioDevicePropertyScopeInput, | 400 kAudioDevicePropertyScopeInput, |
| 404 kAudioObjectPropertyElementMaster | 401 kAudioObjectPropertyElementMaster |
| 405 }; | 402 }; |
| 406 | 403 |
| 407 if (AudioObjectHasProperty(input_device_id_, &property_address)) { | 404 if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
| 408 // The device supports master volume control, get the volume from the | 405 // The device supports master volume control, get the volume from the |
| 409 // master channel. | 406 // master channel. |
| 410 Float32 volume_float32 = 0.0; | 407 Float32 volume_float32 = 0.0; |
| 411 UInt32 size = sizeof(volume_float32); | 408 UInt32 size = sizeof(volume_float32); |
| 412 OSStatus result = AudioObjectGetPropertyData( | 409 OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
| 413 input_device_id_, &property_address, 0, NULL, &size, &volume_float32); | 410 &property_address, |
| 411 0, |
| 412 NULL, |
| 413 &size, |
| 414 &volume_float32); |
| 414 if (result == noErr) | 415 if (result == noErr) |
| 415 return static_cast<double>(volume_float32); | 416 return static_cast<double>(volume_float32); |
| 416 } else { | 417 } else { |
| 417 // There is no master volume control, try to get the average volume of | 418 // There is no master volume control, try to get the average volume of |
| 418 // all the channels. | 419 // all the channels. |
| 419 Float32 volume_float32 = 0.0; | 420 Float32 volume_float32 = 0.0; |
| 420 int successful_channels = 0; | 421 int successful_channels = 0; |
| 421 for (int i = 1; i <= number_of_channels_in_frame_; ++i) { | 422 for (int i = 1; i <= number_of_channels_in_frame_; ++i) { |
| 422 property_address.mElement = static_cast<UInt32>(i); | 423 property_address.mElement = static_cast<UInt32>(i); |
| 423 if (AudioObjectHasProperty(input_device_id_, &property_address)) { | 424 if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 OSStatus result = AudioUnitRender(audio_input->audio_unit(), | 465 OSStatus result = AudioUnitRender(audio_input->audio_unit(), |
| 465 flags, | 466 flags, |
| 466 time_stamp, | 467 time_stamp, |
| 467 bus_number, | 468 bus_number, |
| 468 number_of_frames, | 469 number_of_frames, |
| 469 audio_input->audio_buffer_list()); | 470 audio_input->audio_buffer_list()); |
| 470 if (result) | 471 if (result) |
| 471 return result; | 472 return result; |
| 472 | 473 |
| 473 // Deliver recorded data to the consumer as a callback. | 474 // Deliver recorded data to the consumer as a callback. |
| 474 return audio_input->Provide( | 475 return audio_input->Provide(number_of_frames, |
| 475 number_of_frames, audio_input->audio_buffer_list(), time_stamp); | 476 audio_input->audio_buffer_list(), |
| 477 time_stamp); |
| 476 } | 478 } |
| 477 | 479 |
| 478 OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, | 480 OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, |
| 479 AudioBufferList* io_data, | 481 AudioBufferList* io_data, |
| 480 const AudioTimeStamp* time_stamp) { | 482 const AudioTimeStamp* time_stamp) { |
| 481 // Update the capture latency. | 483 // Update the capture latency. |
| 482 double capture_latency_frames = GetCaptureLatency(time_stamp); | 484 double capture_latency_frames = GetCaptureLatency(time_stamp); |
| 483 | 485 |
| 484 // The AGC volume level is updated once every second on a separate thread. | 486 // The AGC volume level is updated once every second on a separate thread. |
| 485 // Note that, |volume| is also updated each time SetVolume() is called | 487 // Note that, |volume| is also updated each time SetVolume() is called |
| 486 // through IPC by the render-side AGC. | 488 // through IPC by the render-side AGC. |
| 487 double normalized_volume = 0.0; | 489 double normalized_volume = 0.0; |
| 488 GetAgcVolume(&normalized_volume); | 490 GetAgcVolume(&normalized_volume); |
| 489 | 491 |
| 490 AudioBuffer& buffer = io_data->mBuffers[0]; | 492 AudioBuffer& buffer = io_data->mBuffers[0]; |
| 491 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); | 493 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); |
| 492 uint32 capture_delay_bytes = static_cast<uint32>( | 494 uint32 capture_delay_bytes = static_cast<uint32> |
| 493 (capture_latency_frames + 0.5) * format_.mBytesPerFrame); | 495 ((capture_latency_frames + 0.5) * format_.mBytesPerFrame); |
| 494 DCHECK(audio_data); | 496 DCHECK(audio_data); |
| 495 if (!audio_data) | 497 if (!audio_data) |
| 496 return kAudioUnitErr_InvalidElement; | 498 return kAudioUnitErr_InvalidElement; |
| 497 | 499 |
| 498 // If the stream parameters change for any reason, we need to insert a FIFO | 500 // Copy captured (and interleaved) data into FIFO. |
| 499 // since the OnMoreData() pipeline can't handle frame size changes. | 501 fifo_.Push(audio_data, number_of_frames, format_.mBitsPerChannel / 8); |
| 500 if (number_of_frames != number_of_frames_) { | |
| 501 // Create a FIFO on the fly to handle any discrepancies in callback rates. | |
| 502 if (!fifo_) { | |
| 503 fifo_.reset(new AudioBlockFifo(output_bus_->channels(), | |
| 504 number_of_frames_, | |
| 505 kNumberOfBlocksBufferInFifo)); | |
| 506 } | |
| 507 } | |
| 508 | 502 |
| 509 // When FIFO does not kick in, data will be directly passed to the callback. | 503 // Consume and deliver the data when the FIFO has a block of available data. |
| 510 if (!fifo_) { | 504 while (fifo_.available_blocks()) { |
| 511 CHECK_EQ(output_bus_->frames(), static_cast<int>(number_of_frames_)); | 505 const AudioBus* audio_bus = fifo_.Consume(); |
| 512 sink_->OnData( | 506 DCHECK_EQ(audio_bus->frames(), static_cast<int>(number_of_frames_)); |
| 513 this, output_bus_.get(), capture_delay_bytes, normalized_volume); | |
| 514 return noErr; | |
| 515 } | |
| 516 | 507 |
| 517 // Compensate the audio delay caused by the FIFO. | 508 // Compensate the audio delay caused by the FIFO. |
| 518 capture_delay_bytes += fifo_->GetAvailableFrames() * format_.mBytesPerFrame; | 509 capture_delay_bytes += fifo_.GetAvailableFrames() * format_.mBytesPerFrame; |
| 519 | |
| 520 fifo_->Push(output_bus_.get()); | |
| 521 // Consume and deliver the data when the FIFO has a block of available data. | |
| 522 while (fifo_->available_blocks()) { | |
| 523 const AudioBus* audio_bus = fifo_->Consume(); | |
| 524 DCHECK_EQ(audio_bus->frames(), static_cast<int>(number_of_frames_)); | |
| 525 sink_->OnData(this, audio_bus, capture_delay_bytes, normalized_volume); | 510 sink_->OnData(this, audio_bus, capture_delay_bytes, normalized_volume); |
| 526 } | 511 } |
| 527 | 512 |
| 528 return noErr; | 513 return noErr; |
| 529 } | 514 } |
| 530 | 515 |
| 531 int AUAudioInputStream::HardwareSampleRate() { | 516 int AUAudioInputStream::HardwareSampleRate() { |
| 532 // Determine the default input device's sample-rate. | 517 // Determine the default input device's sample-rate. |
| 533 AudioDeviceID device_id = kAudioObjectUnknown; | 518 AudioDeviceID device_id = kAudioObjectUnknown; |
| 534 UInt32 info_size = sizeof(device_id); | 519 UInt32 info_size = sizeof(device_id); |
| 535 | 520 |
| 536 AudioObjectPropertyAddress default_input_device_address = { | 521 AudioObjectPropertyAddress default_input_device_address = { |
| 537 kAudioHardwarePropertyDefaultInputDevice, | 522 kAudioHardwarePropertyDefaultInputDevice, |
| 538 kAudioObjectPropertyScopeGlobal, | 523 kAudioObjectPropertyScopeGlobal, |
| 539 kAudioObjectPropertyElementMaster | 524 kAudioObjectPropertyElementMaster |
| 540 }; | 525 }; |
| 541 OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, | 526 OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 542 &default_input_device_address, | 527 &default_input_device_address, |
| 543 0, | 528 0, |
| 544 0, | 529 0, |
| 545 &info_size, | 530 &info_size, |
| 546 &device_id); | 531 &device_id); |
| 547 if (result != noErr) | 532 if (result != noErr) |
| 548 return 0.0; | 533 return 0.0; |
| 549 | 534 |
| 550 Float64 nominal_sample_rate; | 535 Float64 nominal_sample_rate; |
| 551 info_size = sizeof(nominal_sample_rate); | 536 info_size = sizeof(nominal_sample_rate); |
| 552 | 537 |
| 553 AudioObjectPropertyAddress nominal_sample_rate_address = { | 538 AudioObjectPropertyAddress nominal_sample_rate_address = { |
| 554 kAudioDevicePropertyNominalSampleRate, kAudioObjectPropertyScopeGlobal, | 539 kAudioDevicePropertyNominalSampleRate, |
| 555 kAudioObjectPropertyElementMaster}; | 540 kAudioObjectPropertyScopeGlobal, |
| 541 kAudioObjectPropertyElementMaster |
| 542 }; |
| 556 result = AudioObjectGetPropertyData(device_id, | 543 result = AudioObjectGetPropertyData(device_id, |
| 557 &nominal_sample_rate_address, | 544 &nominal_sample_rate_address, |
| 558 0, | 545 0, |
| 559 0, | 546 0, |
| 560 &info_size, | 547 &info_size, |
| 561 &nominal_sample_rate); | 548 &nominal_sample_rate); |
| 562 if (result != noErr) | 549 if (result != noErr) |
| 563 return 0.0; | 550 return 0.0; |
| 564 | 551 |
| 565 return static_cast<int>(nominal_sample_rate); | 552 return static_cast<int>(nominal_sample_rate); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 578 kAudioUnitProperty_Latency, | 565 kAudioUnitProperty_Latency, |
| 579 kAudioUnitScope_Global, | 566 kAudioUnitScope_Global, |
| 580 0, | 567 0, |
| 581 &audio_unit_latency_sec, | 568 &audio_unit_latency_sec, |
| 582 &size); | 569 &size); |
| 583 OSSTATUS_DLOG_IF(WARNING, result != noErr, result) | 570 OSSTATUS_DLOG_IF(WARNING, result != noErr, result) |
| 584 << "Could not get audio unit latency"; | 571 << "Could not get audio unit latency"; |
| 585 | 572 |
| 586 // Get input audio device latency. | 573 // Get input audio device latency. |
| 587 AudioObjectPropertyAddress property_address = { | 574 AudioObjectPropertyAddress property_address = { |
| 588 kAudioDevicePropertyLatency, | 575 kAudioDevicePropertyLatency, |
| 589 kAudioDevicePropertyScopeInput, | 576 kAudioDevicePropertyScopeInput, |
| 590 kAudioObjectPropertyElementMaster | 577 kAudioObjectPropertyElementMaster |
| 591 }; | 578 }; |
| 592 UInt32 device_latency_frames = 0; | 579 UInt32 device_latency_frames = 0; |
| 593 size = sizeof(device_latency_frames); | 580 size = sizeof(device_latency_frames); |
| 594 result = AudioObjectGetPropertyData(input_device_id_, | 581 result = AudioObjectGetPropertyData(input_device_id_, |
| 595 &property_address, | 582 &property_address, |
| 596 0, | 583 0, |
| 597 NULL, | 584 NULL, |
| 598 &size, | 585 &size, |
| 599 &device_latency_frames); | 586 &device_latency_frames); |
| 600 DLOG_IF(WARNING, result != noErr) << "Could not get audio device latency."; | 587 DLOG_IF(WARNING, result != noErr) << "Could not get audio device latency."; |
| 601 | 588 |
| 602 return static_cast<double>((audio_unit_latency_sec * format_.mSampleRate) + | 589 return static_cast<double>((audio_unit_latency_sec * |
| 603 device_latency_frames); | 590 format_.mSampleRate) + device_latency_frames); |
| 604 } | 591 } |
| 605 | 592 |
| 606 double AUAudioInputStream::GetCaptureLatency( | 593 double AUAudioInputStream::GetCaptureLatency( |
| 607 const AudioTimeStamp* input_time_stamp) { | 594 const AudioTimeStamp* input_time_stamp) { |
| 608 // Get the delay between between the actual recording instant and the time | 595 // Get the delay between between the actual recording instant and the time |
| 609 // when the data packet is provided as a callback. | 596 // when the data packet is provided as a callback. |
| 610 UInt64 capture_time_ns = | 597 UInt64 capture_time_ns = AudioConvertHostTimeToNanos( |
| 611 AudioConvertHostTimeToNanos(input_time_stamp->mHostTime); | 598 input_time_stamp->mHostTime); |
| 612 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); | 599 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
| 613 double delay_frames = static_cast<double>(1e-9 * (now_ns - capture_time_ns) * | 600 double delay_frames = static_cast<double> |
| 614 format_.mSampleRate); | 601 (1e-9 * (now_ns - capture_time_ns) * format_.mSampleRate); |
| 615 | 602 |
| 616 // Total latency is composed by the dynamic latency and the fixed | 603 // Total latency is composed by the dynamic latency and the fixed |
| 617 // hardware latency. | 604 // hardware latency. |
| 618 return (delay_frames + hardware_latency_frames_); | 605 return (delay_frames + hardware_latency_frames_); |
| 619 } | 606 } |
| 620 | 607 |
| 621 int AUAudioInputStream::GetNumberOfChannelsFromStream() { | 608 int AUAudioInputStream::GetNumberOfChannelsFromStream() { |
| 622 // Get the stream format, to be able to read the number of channels. | 609 // Get the stream format, to be able to read the number of channels. |
| 623 AudioObjectPropertyAddress property_address = { | 610 AudioObjectPropertyAddress property_address = { |
| 624 kAudioDevicePropertyStreamFormat, | 611 kAudioDevicePropertyStreamFormat, |
| 625 kAudioDevicePropertyScopeInput, | 612 kAudioDevicePropertyScopeInput, |
| 626 kAudioObjectPropertyElementMaster | 613 kAudioObjectPropertyElementMaster |
| 627 }; | 614 }; |
| 628 AudioStreamBasicDescription stream_format; | 615 AudioStreamBasicDescription stream_format; |
| 629 UInt32 size = sizeof(stream_format); | 616 UInt32 size = sizeof(stream_format); |
| 630 OSStatus result = AudioObjectGetPropertyData( | 617 OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
| 631 input_device_id_, &property_address, 0, NULL, &size, &stream_format); | 618 &property_address, |
| 619 0, |
| 620 NULL, |
| 621 &size, |
| 622 &stream_format); |
| 632 if (result != noErr) { | 623 if (result != noErr) { |
| 633 DLOG(WARNING) << "Could not get stream format"; | 624 DLOG(WARNING) << "Could not get stream format"; |
| 634 return 0; | 625 return 0; |
| 635 } | 626 } |
| 636 | 627 |
| 637 return static_cast<int>(stream_format.mChannelsPerFrame); | 628 return static_cast<int>(stream_format.mChannelsPerFrame); |
| 638 } | 629 } |
| 639 | 630 |
| 640 void AUAudioInputStream::HandleError(OSStatus err) { | 631 void AUAudioInputStream::HandleError(OSStatus err) { |
| 641 NOTREACHED() << "error " << GetMacOSStatusErrorString(err) << " (" << err | 632 NOTREACHED() << "error " << GetMacOSStatusErrorString(err) |
| 642 << ")"; | 633 << " (" << err << ")"; |
| 643 if (sink_) | 634 if (sink_) |
| 644 sink_->OnError(this); | 635 sink_->OnError(this); |
| 645 } | 636 } |
| 646 | 637 |
| 647 bool AUAudioInputStream::IsVolumeSettableOnChannel(int channel) { | 638 bool AUAudioInputStream::IsVolumeSettableOnChannel(int channel) { |
| 648 Boolean is_settable = false; | 639 Boolean is_settable = false; |
| 649 AudioObjectPropertyAddress property_address = { | 640 AudioObjectPropertyAddress property_address = { |
| 650 kAudioDevicePropertyVolumeScalar, | 641 kAudioDevicePropertyVolumeScalar, |
| 651 kAudioDevicePropertyScopeInput, | 642 kAudioDevicePropertyScopeInput, |
| 652 static_cast<UInt32>(channel) | 643 static_cast<UInt32>(channel) |
| 653 }; | 644 }; |
| 654 OSStatus result = AudioObjectIsPropertySettable( | 645 OSStatus result = AudioObjectIsPropertySettable(input_device_id_, |
| 655 input_device_id_, &property_address, &is_settable); | 646 &property_address, |
| 647 &is_settable); |
| 656 return (result == noErr) ? is_settable : false; | 648 return (result == noErr) ? is_settable : false; |
| 657 } | 649 } |
| 658 | 650 |
| 659 } // namespace media | 651 } // namespace media |
| OLD | NEW |