Chromium Code Reviews| Index: media/audio/mac/audio_low_latency_input_mac.cc |
| =================================================================== |
| --- media/audio/mac/audio_low_latency_input_mac.cc (revision 104447) |
| +++ media/audio/mac/audio_low_latency_input_mac.cc (working copy) |
| @@ -31,7 +31,10 @@ |
| : manager_(manager), |
| sink_(NULL), |
| audio_unit_(0), |
| - started_(false) { |
| + input_device_id_(kAudioDeviceUnknown), |
| + started_(false), |
| + hardware_latency_frames_(0), |
| + capture_latency_frames_(0) { |
| DCHECK(manager_); |
| // Set up the desired (output) format specified by the client. |
| @@ -136,7 +139,7 @@ |
| // Set the current device of the AudioOuputUnit to default input device. |
| - AudioDeviceID input_device; |
| + AudioDeviceID input_device = kAudioDeviceUnknown; |
| UInt32 size = sizeof(input_device); |
| // First, obtain the current input device selected by the user. |
| @@ -148,13 +151,16 @@ |
| return false; |
| } |
| + // Store the input device id. |
| + input_device_id_ = input_device; |
| + |
| // Next, set the audio device to be the Audio Unit's current device. |
| // Note that, devices can only be set to the AUHAL after enabling IO. |
| result = AudioUnitSetProperty(audio_unit_, |
| kAudioOutputUnitProperty_CurrentDevice, |
| kAudioUnitScope_Global, |
| 0, |
| - &input_device, |
| + &input_device_id_, |
| sizeof(input_device)); |
| if (result) { |
| HandleError(result); |
| @@ -212,6 +218,10 @@ |
| HandleError(result); |
| return false; |
| } |
| + |
| + // Gets the capture device hardware latency and stores it. |
| + StoreHardwareLatency(); |
|
scherkus (not reviewing)
2011/10/19 16:35:15
functions that have side effects (i.e., setting th
no longer working on chromium
2011/10/19 18:19:05
I took the 2nd solution.
It is more like a warnin
|
| + |
| return true; |
| } |
| @@ -289,19 +299,25 @@ |
| // Deliver recorded data to the consumer as a callback. |
| return audio_input->Provide(number_of_frames, |
| - audio_input->audio_buffer_list()); |
| + audio_input->audio_buffer_list(), |
| + time_stamp); |
| } |
| OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, |
| - AudioBufferList* io_data) { |
| + AudioBufferList* io_data, |
| + const AudioTimeStamp* time_stamp) { |
| + // Update the capture latency. |
| + UpdateCaptureLatency(time_stamp); |
|
scherkus (not reviewing)
2011/10/19 16:35:15
functions that have side effects like this can be
no longer working on chromium
2011/10/19 18:19:05
Good idea, done.
|
| + |
| AudioBuffer& buffer = io_data->mBuffers[0]; |
| uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); |
| + uint32 capture_delay_bytes = static_cast<uint32> |
| + (capture_latency_frames_ * format_.mBytesPerFrame + 0.5); |
| DCHECK(audio_data); |
| if (!audio_data) |
| return kAudioUnitErr_InvalidElement; |
| - // TODO(henrika): improve delay estimation. Using buffer size for now. |
| - sink_->OnData(this, audio_data, buffer.mDataByteSize, buffer.mDataByteSize); |
| + sink_->OnData(this, audio_data, buffer.mDataByteSize, capture_delay_bytes); |
| return noErr; |
| } |
| @@ -347,6 +363,77 @@ |
| return nominal_sample_rate; |
| } |
| +void AUAudioInputStream::StoreHardwareLatency() { |
| + // Get audio unit latency. |
| + Float64 audio_unit_latency_sec = 0.0; |
| + UInt32 size = sizeof(audio_unit_latency_sec); |
| + OSStatus result = AudioUnitGetProperty(audio_unit_, |
| + kAudioUnitProperty_Latency, |
| + kAudioUnitScope_Global, |
| + 0, |
| + &audio_unit_latency_sec, |
| + &size); |
| + if (result) |
| + DLOG(WARNING) << "StoreHardwareLatency: Could not get audio unit latency."; |
| + |
| + // Get audio device latency. |
| + UInt32 device_latency_frames = 0; |
| + size = sizeof(device_latency_frames); |
| + result = AudioDeviceGetProperty(input_device_id_, 0, |
| + true, |
| + kAudioDevicePropertyLatency, |
| + &size, |
| + &device_latency_frames); |
| + if (result) |
| + DLOG(WARNING) << "StoreHardwareLatency: Could not get device latency."; |
| + |
| + // Get the stream latency. |
| + UInt32 stream_latency_frames = 0; |
| + size = 0; |
| + result = AudioDeviceGetPropertyInfo(input_device_id_, |
| + 0, |
| + true, |
| + kAudioDevicePropertyStreams, |
| + &size, |
| + NULL); |
| + if (!result) { |
| + scoped_ptr_malloc<AudioStreamID> |
| + streams(reinterpret_cast<AudioStreamID*>(malloc(size))); |
| + AudioStreamID* stream_ids = streams.get(); |
| + result = AudioDeviceGetProperty(input_device_id_, |
| + 0, |
| + true, |
| + kAudioDevicePropertyStreams, |
| + &size, |
| + stream_ids); |
| + if (!result) |
| + result = AudioStreamGetProperty(stream_ids[0], |
| + 0, |
| + kAudioStreamPropertyLatency, |
| + &size, |
| + &stream_latency_frames); |
| + } |
| + // Logs the warning if it fails to get the stream latency. |
| + if (result) |
| + DLOG(WARNING) << "StoreHardwareLatency: Could not get stream latency."; |
| + |
| + // Store the hardware latency value in frames. |
| + hardware_latency_frames_ = static_cast<double>(audio_unit_latency_sec * |
| + format_.mSampleRate + device_latency_frames + stream_latency_frames); |
| +} |
| + |
| +void AUAudioInputStream::UpdateCaptureLatency( |
| + const AudioTimeStamp* input_time_stamp) { |
| + // Get the delay between now and when the data was reaching the hardware. |
| + UInt64 input_time_ns = AudioConvertHostTimeToNanos( |
| + input_time_stamp->mHostTime); |
| + UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
| + double delay_frames = static_cast<double> |
| + (1e-9 * (now_ns - input_time_ns) * format_.mSampleRate); |
| + |
| + capture_latency_frames_ = delay_frames + hardware_latency_frames_; |
| +} |
| + |
| void AUAudioInputStream::HandleError(OSStatus err) { |
| NOTREACHED() << "error code: " << err; |
| if (sink_) |