OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 | 24 |
25 // See "Technical Note TN2091 - Device input using the HAL Output Audio Unit" | 25 // See "Technical Note TN2091 - Device input using the HAL Output Audio Unit" |
26 // http://developer.apple.com/library/mac/#technotes/tn2091/_index.html | 26 // http://developer.apple.com/library/mac/#technotes/tn2091/_index.html |
27 // for more details and background regarding this implementation. | 27 // for more details and background regarding this implementation. |
28 | 28 |
29 AUAudioInputStream::AUAudioInputStream( | 29 AUAudioInputStream::AUAudioInputStream( |
30 AudioManagerMac* manager, const AudioParameters& params) | 30 AudioManagerMac* manager, const AudioParameters& params) |
31 : manager_(manager), | 31 : manager_(manager), |
32 sink_(NULL), | 32 sink_(NULL), |
33 audio_unit_(0), | 33 audio_unit_(0), |
34 started_(false) { | 34 input_device_id_(kAudioDeviceUnknown), |
35 started_(false), | |
36 hardware_latency_frames_(0), | |
37 capture_latency_frames_(0) { | |
35 DCHECK(manager_); | 38 DCHECK(manager_); |
36 | 39 |
37 // Set up the desired (output) format specified by the client. | 40 // Set up the desired (output) format specified by the client. |
38 format_.mSampleRate = params.sample_rate; | 41 format_.mSampleRate = params.sample_rate; |
39 format_.mFormatID = kAudioFormatLinearPCM; | 42 format_.mFormatID = kAudioFormatLinearPCM; |
40 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | | 43 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
41 kLinearPCMFormatFlagIsSignedInteger; | 44 kLinearPCMFormatFlagIsSignedInteger; |
42 format_.mBitsPerChannel = params.bits_per_sample; | 45 format_.mBitsPerChannel = params.bits_per_sample; |
43 format_.mChannelsPerFrame = params.channels; | 46 format_.mChannelsPerFrame = params.channels; |
44 format_.mFramesPerPacket = 1; // uncompressed audio | 47 format_.mFramesPerPacket = 1; // uncompressed audio |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 0, // output element 0 | 132 0, // output element 0 |
130 &enableIO, // disable | 133 &enableIO, // disable |
131 sizeof(enableIO)); | 134 sizeof(enableIO)); |
132 if (result) { | 135 if (result) { |
133 HandleError(result); | 136 HandleError(result); |
134 return false; | 137 return false; |
135 } | 138 } |
136 | 139 |
137 // Set the current device of the AudioOuputUnit to default input device. | 140 // Set the current device of the AudioOuputUnit to default input device. |
138 | 141 |
139 AudioDeviceID input_device; | 142 AudioDeviceID input_device = kAudioDeviceUnknown; |
140 UInt32 size = sizeof(input_device); | 143 UInt32 size = sizeof(input_device); |
141 | 144 |
142 // First, obtain the current input device selected by the user. | 145 // First, obtain the current input device selected by the user. |
143 result = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, | 146 result = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, |
144 &size, | 147 &size, |
145 &input_device); | 148 &input_device); |
146 if (result) { | 149 if (result) { |
147 HandleError(result); | 150 HandleError(result); |
148 return false; | 151 return false; |
149 } | 152 } |
150 | 153 |
154 // Store the input device id. | |
155 input_device_id_ = input_device; | |
156 | |
151 // Next, set the audio device to be the Audio Unit's current device. | 157 // Next, set the audio device to be the Audio Unit's current device. |
152 // Note that, devices can only be set to the AUHAL after enabling IO. | 158 // Note that, devices can only be set to the AUHAL after enabling IO. |
153 result = AudioUnitSetProperty(audio_unit_, | 159 result = AudioUnitSetProperty(audio_unit_, |
154 kAudioOutputUnitProperty_CurrentDevice, | 160 kAudioOutputUnitProperty_CurrentDevice, |
155 kAudioUnitScope_Global, | 161 kAudioUnitScope_Global, |
156 0, | 162 0, |
157 &input_device, | 163 &input_device_id_, |
158 sizeof(input_device)); | 164 sizeof(input_device)); |
159 if (result) { | 165 if (result) { |
160 HandleError(result); | 166 HandleError(result); |
161 return false; | 167 return false; |
162 } | 168 } |
163 | 169 |
164 // Register the input procedure for the AUHAL. | 170 // Register the input procedure for the AUHAL. |
165 // This procedure will be called when the AUHAL has received new data | 171 // This procedure will be called when the AUHAL has received new data |
166 // from the input device. | 172 // from the input device. |
167 AURenderCallbackStruct callback; | 173 AURenderCallbackStruct callback; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 } | 211 } |
206 | 212 |
207 // Finally, initialize the audio unit and ensure that it is ready to render. | 213 // Finally, initialize the audio unit and ensure that it is ready to render. |
208 // Allocates memory according to the maximum number of audio frames | 214 // Allocates memory according to the maximum number of audio frames |
209 // it can produce in response to a single render call. | 215 // it can produce in response to a single render call. |
210 result = AudioUnitInitialize(audio_unit_); | 216 result = AudioUnitInitialize(audio_unit_); |
211 if (result) { | 217 if (result) { |
212 HandleError(result); | 218 HandleError(result); |
213 return false; | 219 return false; |
214 } | 220 } |
221 | |
222 // Gets the capture device hardware latency and stores it. | |
223 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
| |
224 | |
215 return true; | 225 return true; |
216 } | 226 } |
217 | 227 |
218 void AUAudioInputStream::Start(AudioInputCallback* callback) { | 228 void AUAudioInputStream::Start(AudioInputCallback* callback) { |
219 DCHECK(callback); | 229 DCHECK(callback); |
220 if (started_) | 230 if (started_) |
221 return; | 231 return; |
222 sink_ = callback; | 232 sink_ = callback; |
223 OSStatus result = AudioOutputUnitStart(audio_unit_); | 233 OSStatus result = AudioOutputUnitStart(audio_unit_); |
224 if (result == noErr) { | 234 if (result == noErr) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 flags, | 292 flags, |
283 time_stamp, | 293 time_stamp, |
284 bus_number, | 294 bus_number, |
285 number_of_frames, | 295 number_of_frames, |
286 audio_input->audio_buffer_list()); | 296 audio_input->audio_buffer_list()); |
287 if (result) | 297 if (result) |
288 return result; | 298 return result; |
289 | 299 |
290 // Deliver recorded data to the consumer as a callback. | 300 // Deliver recorded data to the consumer as a callback. |
291 return audio_input->Provide(number_of_frames, | 301 return audio_input->Provide(number_of_frames, |
292 audio_input->audio_buffer_list()); | 302 audio_input->audio_buffer_list(), |
303 time_stamp); | |
293 } | 304 } |
294 | 305 |
295 OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, | 306 OSStatus AUAudioInputStream::Provide(UInt32 number_of_frames, |
296 AudioBufferList* io_data) { | 307 AudioBufferList* io_data, |
308 const AudioTimeStamp* time_stamp) { | |
309 // Update the capture latency. | |
310 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.
| |
311 | |
297 AudioBuffer& buffer = io_data->mBuffers[0]; | 312 AudioBuffer& buffer = io_data->mBuffers[0]; |
298 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); | 313 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); |
314 uint32 capture_delay_bytes = static_cast<uint32> | |
315 (capture_latency_frames_ * format_.mBytesPerFrame + 0.5); | |
299 DCHECK(audio_data); | 316 DCHECK(audio_data); |
300 if (!audio_data) | 317 if (!audio_data) |
301 return kAudioUnitErr_InvalidElement; | 318 return kAudioUnitErr_InvalidElement; |
302 | 319 |
303 // TODO(henrika): improve delay estimation. Using buffer size for now. | 320 sink_->OnData(this, audio_data, buffer.mDataByteSize, capture_delay_bytes); |
304 sink_->OnData(this, audio_data, buffer.mDataByteSize, buffer.mDataByteSize); | |
305 | 321 |
306 return noErr; | 322 return noErr; |
307 } | 323 } |
308 | 324 |
309 double AUAudioInputStream::HardwareSampleRate() { | 325 double AUAudioInputStream::HardwareSampleRate() { |
310 // Determine the default input device's sample-rate. | 326 // Determine the default input device's sample-rate. |
311 AudioDeviceID device_id = kAudioDeviceUnknown; | 327 AudioDeviceID device_id = kAudioDeviceUnknown; |
312 UInt32 info_size = sizeof(device_id); | 328 UInt32 info_size = sizeof(device_id); |
313 | 329 |
314 AudioObjectPropertyAddress default_input_device_address = { | 330 AudioObjectPropertyAddress default_input_device_address = { |
(...skipping 25 matching lines...) Expand all Loading... | |
340 0, | 356 0, |
341 &info_size, | 357 &info_size, |
342 &nominal_sample_rate); | 358 &nominal_sample_rate); |
343 DCHECK_EQ(result, 0); | 359 DCHECK_EQ(result, 0); |
344 if (result) | 360 if (result) |
345 return 0.0; | 361 return 0.0; |
346 | 362 |
347 return nominal_sample_rate; | 363 return nominal_sample_rate; |
348 } | 364 } |
349 | 365 |
366 void AUAudioInputStream::StoreHardwareLatency() { | |
367 // Get audio unit latency. | |
368 Float64 audio_unit_latency_sec = 0.0; | |
369 UInt32 size = sizeof(audio_unit_latency_sec); | |
370 OSStatus result = AudioUnitGetProperty(audio_unit_, | |
371 kAudioUnitProperty_Latency, | |
372 kAudioUnitScope_Global, | |
373 0, | |
374 &audio_unit_latency_sec, | |
375 &size); | |
376 if (result) | |
377 DLOG(WARNING) << "StoreHardwareLatency: Could not get audio unit latency."; | |
378 | |
379 // Get audio device latency. | |
380 UInt32 device_latency_frames = 0; | |
381 size = sizeof(device_latency_frames); | |
382 result = AudioDeviceGetProperty(input_device_id_, 0, | |
383 true, | |
384 kAudioDevicePropertyLatency, | |
385 &size, | |
386 &device_latency_frames); | |
387 if (result) | |
388 DLOG(WARNING) << "StoreHardwareLatency: Could not get device latency."; | |
389 | |
390 // Get the stream latency. | |
391 UInt32 stream_latency_frames = 0; | |
392 size = 0; | |
393 result = AudioDeviceGetPropertyInfo(input_device_id_, | |
394 0, | |
395 true, | |
396 kAudioDevicePropertyStreams, | |
397 &size, | |
398 NULL); | |
399 if (!result) { | |
400 scoped_ptr_malloc<AudioStreamID> | |
401 streams(reinterpret_cast<AudioStreamID*>(malloc(size))); | |
402 AudioStreamID* stream_ids = streams.get(); | |
403 result = AudioDeviceGetProperty(input_device_id_, | |
404 0, | |
405 true, | |
406 kAudioDevicePropertyStreams, | |
407 &size, | |
408 stream_ids); | |
409 if (!result) | |
410 result = AudioStreamGetProperty(stream_ids[0], | |
411 0, | |
412 kAudioStreamPropertyLatency, | |
413 &size, | |
414 &stream_latency_frames); | |
415 } | |
416 // Logs the warning if it fails to get the stream latency. | |
417 if (result) | |
418 DLOG(WARNING) << "StoreHardwareLatency: Could not get stream latency."; | |
419 | |
420 // Store the hardware latency value in frames. | |
421 hardware_latency_frames_ = static_cast<double>(audio_unit_latency_sec * | |
422 format_.mSampleRate + device_latency_frames + stream_latency_frames); | |
423 } | |
424 | |
425 void AUAudioInputStream::UpdateCaptureLatency( | |
426 const AudioTimeStamp* input_time_stamp) { | |
427 // Get the delay between now and when the data was reaching the hardware. | |
428 UInt64 input_time_ns = AudioConvertHostTimeToNanos( | |
429 input_time_stamp->mHostTime); | |
430 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); | |
431 double delay_frames = static_cast<double> | |
432 (1e-9 * (now_ns - input_time_ns) * format_.mSampleRate); | |
433 | |
434 capture_latency_frames_ = delay_frames + hardware_latency_frames_; | |
435 } | |
436 | |
350 void AUAudioInputStream::HandleError(OSStatus err) { | 437 void AUAudioInputStream::HandleError(OSStatus err) { |
351 NOTREACHED() << "error code: " << err; | 438 NOTREACHED() << "error code: " << err; |
352 if (sink_) | 439 if (sink_) |
353 sink_->OnError(this, static_cast<int>(err)); | 440 sink_->OnError(this, static_cast<int>(err)); |
354 } | 441 } |
OLD | NEW |