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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // for more details and background regarding this implementation. | 30 // for more details and background regarding this implementation. |
31 | 31 |
32 AUAudioInputStream::AUAudioInputStream( | 32 AUAudioInputStream::AUAudioInputStream( |
33 AudioManagerMac* manager, const AudioParameters& params, | 33 AudioManagerMac* manager, const AudioParameters& params, |
34 AudioDeviceID audio_device_id) | 34 AudioDeviceID audio_device_id) |
35 : manager_(manager), | 35 : manager_(manager), |
36 sink_(NULL), | 36 sink_(NULL), |
37 audio_unit_(0), | 37 audio_unit_(0), |
38 input_device_id_(audio_device_id), | 38 input_device_id_(audio_device_id), |
39 started_(false), | 39 started_(false), |
40 hardware_latency_frames_(0) { | 40 hardware_latency_frames_(0), |
| 41 number_of_channels_in_frame_(0) { |
41 DCHECK(manager_); | 42 DCHECK(manager_); |
42 | 43 |
43 // Set up the desired (output) format specified by the client. | 44 // Set up the desired (output) format specified by the client. |
44 format_.mSampleRate = params.sample_rate; | 45 format_.mSampleRate = params.sample_rate; |
45 format_.mFormatID = kAudioFormatLinearPCM; | 46 format_.mFormatID = kAudioFormatLinearPCM; |
46 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | | 47 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
47 kLinearPCMFormatFlagIsSignedInteger; | 48 kLinearPCMFormatFlagIsSignedInteger; |
48 format_.mBitsPerChannel = params.bits_per_sample; | 49 format_.mBitsPerChannel = params.bits_per_sample; |
49 format_.mChannelsPerFrame = params.channels; | 50 format_.mChannelsPerFrame = params.channels; |
50 format_.mFramesPerPacket = 1; // uncompressed audio | 51 format_.mFramesPerPacket = 1; // uncompressed audio |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // it can produce in response to a single render call. | 205 // it can produce in response to a single render call. |
205 result = AudioUnitInitialize(audio_unit_); | 206 result = AudioUnitInitialize(audio_unit_); |
206 if (result) { | 207 if (result) { |
207 HandleError(result); | 208 HandleError(result); |
208 return false; | 209 return false; |
209 } | 210 } |
210 | 211 |
211 // The hardware latency is fixed and will not change during the call. | 212 // The hardware latency is fixed and will not change during the call. |
212 hardware_latency_frames_ = GetHardwareLatency(); | 213 hardware_latency_frames_ = GetHardwareLatency(); |
213 | 214 |
| 215 number_of_channels_in_frame_ = GetNumberOfChannelsFromStream(); |
| 216 |
214 return true; | 217 return true; |
215 } | 218 } |
216 | 219 |
217 void AUAudioInputStream::Start(AudioInputCallback* callback) { | 220 void AUAudioInputStream::Start(AudioInputCallback* callback) { |
218 DCHECK(callback); | 221 DCHECK(callback); |
219 DLOG_IF(ERROR, !audio_unit_) << "Open() has not been called successfully"; | 222 DLOG_IF(ERROR, !audio_unit_) << "Open() has not been called successfully"; |
220 if (started_ || !audio_unit_) | 223 if (started_ || !audio_unit_) |
221 return; | 224 return; |
222 sink_ = callback; | 225 sink_ = callback; |
223 OSStatus result = AudioOutputUnitStart(audio_unit_); | 226 OSStatus result = AudioOutputUnitStart(audio_unit_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if (sink_) { | 259 if (sink_) { |
257 sink_->OnClose(this); | 260 sink_->OnClose(this); |
258 sink_ = NULL; | 261 sink_ = NULL; |
259 } | 262 } |
260 | 263 |
261 // Inform the audio manager that we have been closed. This can cause our | 264 // Inform the audio manager that we have been closed. This can cause our |
262 // destruction. | 265 // destruction. |
263 manager_->ReleaseInputStream(this); | 266 manager_->ReleaseInputStream(this); |
264 } | 267 } |
265 | 268 |
| 269 void AUAudioInputStream::GetMaxMicVolume(double* volume) { |
| 270 // Verify that we have a valid device. |
| 271 if (input_device_id_ == kAudioObjectUnknown) |
| 272 return; |
| 273 |
| 274 if (IsVolumeSettableOnChannel(0)) { |
| 275 // If the volume is settable, the valid volume range is [0.0, 1.0]. |
| 276 *volume = 1.0; |
| 277 return; |
| 278 } |
| 279 |
| 280 // There is no master volume control, try to set volume for each channel. |
| 281 for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
| 282 if (IsVolumeSettableOnChannel(i)) { |
| 283 *volume = 1.0; |
| 284 return; |
| 285 } |
| 286 } |
| 287 |
| 288 // Volume control is not available for the audio stream. |
| 289 *volume = 0.0; |
| 290 } |
| 291 |
| 292 void AUAudioInputStream::SetMicVolume(double volume) { |
| 293 DCHECK(volume <= 1.0 && volume >= 0.0); |
| 294 |
| 295 // Verify that we have a valid device. |
| 296 if (input_device_id_ == kAudioObjectUnknown) |
| 297 return; |
| 298 |
| 299 Float32 volume_float32 = static_cast<Float32> (volume); |
| 300 AudioObjectPropertyAddress property_address = { |
| 301 kAudioDevicePropertyVolumeScalar, |
| 302 kAudioDevicePropertyScopeInput, |
| 303 kAudioObjectPropertyElementMaster |
| 304 }; |
| 305 |
| 306 // Try to set the volume for master volume channel. |
| 307 if (IsVolumeSettableOnChannel(kAudioObjectPropertyElementMaster)) { |
| 308 UInt32 size = sizeof(volume_float32); |
| 309 OSStatus result = AudioObjectSetPropertyData(input_device_id_, |
| 310 &property_address, |
| 311 0, |
| 312 NULL, |
| 313 size, |
| 314 &volume_float32); |
| 315 DLOG_IF(WARNING, result != noErr) << "SetMicVolume failed to set volume to " |
| 316 << volume_float32; |
| 317 return; |
| 318 } |
| 319 |
| 320 // There is no master volume control, try to set volume for each channel. |
| 321 int success_on_channel = 0; |
| 322 for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
| 323 property_address.mElement = i; |
| 324 if (IsVolumeSettableOnChannel(i)) { |
| 325 UInt32 size = sizeof(volume_float32); |
| 326 OSStatus result = AudioObjectSetPropertyData(input_device_id_, |
| 327 &property_address, |
| 328 0, |
| 329 NULL, |
| 330 size, |
| 331 &volume_float32); |
| 332 if (result == noErr) |
| 333 ++success_on_channel; |
| 334 } |
| 335 } |
| 336 |
| 337 DLOG_IF(WARNING, success_on_channel == noErr) |
| 338 << "SetMicVolume failed to set volume to " << volume_float32; |
| 339 } |
| 340 |
| 341 void AUAudioInputStream::GetMicVolume(double* volume) { |
| 342 // Verify that we have a valid device. |
| 343 if (input_device_id_ == kAudioObjectUnknown) |
| 344 return; |
| 345 |
| 346 *volume = 0.0; |
| 347 AudioObjectPropertyAddress property_address = { |
| 348 kAudioDevicePropertyVolumeScalar, |
| 349 kAudioDevicePropertyScopeInput, |
| 350 kAudioObjectPropertyElementMaster |
| 351 }; |
| 352 |
| 353 if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
| 354 // The device supports master volume control, get the volume from the |
| 355 // master channel. |
| 356 Float32 volume_float32 = 0.0; |
| 357 UInt32 size = sizeof(volume_float32); |
| 358 OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
| 359 &property_address, |
| 360 0, |
| 361 NULL, |
| 362 &size, |
| 363 &volume_float32); |
| 364 if (result != noErr) { |
| 365 DLOG(WARNING) << "AudioObjectGetPropertyData failed not get volume."; |
| 366 return; |
| 367 } |
| 368 *volume = static_cast<double> (volume_float32); |
| 369 } else { |
| 370 // There is no master volume control, try to get the average volume of |
| 371 // all the channels. |
| 372 Float32 volume_float32 = 0.0; |
| 373 int success = 0; |
| 374 for (UInt32 i = 1; i <= number_of_channels_in_frame_; i++) { |
| 375 property_address.mElement = i; |
| 376 if (AudioObjectHasProperty(input_device_id_, &property_address)) { |
| 377 Float32 channel_volume = 0; |
| 378 UInt32 size = sizeof(channel_volume); |
| 379 OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
| 380 &property_address, |
| 381 0, |
| 382 NULL, |
| 383 &size, |
| 384 &channel_volume); |
| 385 if (result == noErr) { |
| 386 volume_float32 += channel_volume; |
| 387 ++success; |
| 388 } |
| 389 } |
| 390 } |
| 391 if (success == 0) { |
| 392 DLOG(WARNING) << "Unable to get volume from any channel"; |
| 393 return; |
| 394 } |
| 395 |
| 396 // Get the average volume of the channels. |
| 397 *volume = static_cast<double> (volume_float32 / success); |
| 398 } |
| 399 } |
| 400 |
266 // AUHAL AudioDeviceOutput unit callback | 401 // AUHAL AudioDeviceOutput unit callback |
267 OSStatus AUAudioInputStream::InputProc(void* user_data, | 402 OSStatus AUAudioInputStream::InputProc(void* user_data, |
268 AudioUnitRenderActionFlags* flags, | 403 AudioUnitRenderActionFlags* flags, |
269 const AudioTimeStamp* time_stamp, | 404 const AudioTimeStamp* time_stamp, |
270 UInt32 bus_number, | 405 UInt32 bus_number, |
271 UInt32 number_of_frames, | 406 UInt32 number_of_frames, |
272 AudioBufferList* io_data) { | 407 AudioBufferList* io_data) { |
273 // Verify that the correct bus is used (Input bus/Element 1) | 408 // Verify that the correct bus is used (Input bus/Element 1) |
274 DCHECK_EQ(bus_number, static_cast<UInt32>(1)); | 409 DCHECK_EQ(bus_number, static_cast<UInt32>(1)); |
275 AUAudioInputStream* audio_input = | 410 AUAudioInputStream* audio_input = |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 input_time_stamp->mHostTime); | 566 input_time_stamp->mHostTime); |
432 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); | 567 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
433 double delay_frames = static_cast<double> | 568 double delay_frames = static_cast<double> |
434 (1e-9 * (now_ns - capture_time_ns) * format_.mSampleRate); | 569 (1e-9 * (now_ns - capture_time_ns) * format_.mSampleRate); |
435 | 570 |
436 // Total latency is composed by the dynamic latency and the fixed | 571 // Total latency is composed by the dynamic latency and the fixed |
437 // hardware latency. | 572 // hardware latency. |
438 return (delay_frames + hardware_latency_frames_); | 573 return (delay_frames + hardware_latency_frames_); |
439 } | 574 } |
440 | 575 |
| 576 UInt32 AUAudioInputStream::GetNumberOfChannelsFromStream() { |
| 577 // Get the stream format, to be able to read the number of channels. |
| 578 AudioObjectPropertyAddress property_address = { |
| 579 kAudioDevicePropertyStreamFormat, |
| 580 kAudioDevicePropertyScopeInput, |
| 581 kAudioObjectPropertyElementMaster |
| 582 }; |
| 583 AudioStreamBasicDescription stream_format; |
| 584 UInt32 size = sizeof(stream_format); |
| 585 OSStatus result = AudioObjectGetPropertyData(input_device_id_, |
| 586 &property_address, |
| 587 0, |
| 588 NULL, |
| 589 &size, |
| 590 &stream_format); |
| 591 DLOG_IF(WARNING, result != noErr) << "Could not get stream format."; |
| 592 |
| 593 return stream_format.mChannelsPerFrame; |
| 594 } |
| 595 |
441 void AUAudioInputStream::HandleError(OSStatus err) { | 596 void AUAudioInputStream::HandleError(OSStatus err) { |
442 NOTREACHED() << "error " << GetMacOSStatusErrorString(err) | 597 NOTREACHED() << "error " << GetMacOSStatusErrorString(err) |
443 << " (" << err << ")"; | 598 << " (" << err << ")"; |
444 if (sink_) | 599 if (sink_) |
445 sink_->OnError(this, static_cast<int>(err)); | 600 sink_->OnError(this, static_cast<int>(err)); |
446 } | 601 } |
| 602 |
| 603 bool AUAudioInputStream::IsVolumeSettableOnChannel(int channel) { |
| 604 Boolean is_settable = false; |
| 605 AudioObjectPropertyAddress property_address = { |
| 606 kAudioDevicePropertyVolumeScalar, |
| 607 kAudioDevicePropertyScopeInput, |
| 608 kAudioObjectPropertyElementMaster |
| 609 }; |
| 610 property_address.mElement = channel; |
| 611 OSStatus result = AudioObjectIsPropertySettable(input_device_id_, |
| 612 &property_address, |
| 613 &is_settable); |
| 614 return (result == noErr) ? is_settable : false; |
| 615 } |
OLD | NEW |