| 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 <CoreAudio/AudioHardware.h> | 5 #include <CoreAudio/AudioHardware.h> |
| 6 | 6 |
| 7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #include "base/mac/scoped_cftyperef.h" |
| 8 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 9 #include "media/audio/fake_audio_input_stream.h" | 10 #include "media/audio/fake_audio_input_stream.h" |
| 10 #include "media/audio/fake_audio_output_stream.h" | 11 #include "media/audio/fake_audio_output_stream.h" |
| 11 #include "media/audio/mac/audio_input_mac.h" | 12 #include "media/audio/mac/audio_input_mac.h" |
| 12 #include "media/audio/mac/audio_low_latency_input_mac.h" | 13 #include "media/audio/mac/audio_low_latency_input_mac.h" |
| 13 #include "media/audio/mac/audio_low_latency_output_mac.h" | 14 #include "media/audio/mac/audio_low_latency_output_mac.h" |
| 14 #include "media/audio/mac/audio_manager_mac.h" | 15 #include "media/audio/mac/audio_manager_mac.h" |
| 15 #include "media/audio/mac/audio_output_mac.h" | 16 #include "media/audio/mac/audio_output_mac.h" |
| 16 #include "media/base/limits.h" | 17 #include "media/base/limits.h" |
| 17 | 18 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // We are responsible for releasing the returned CFObject. See the | 176 // We are responsible for releasing the returned CFObject. See the |
| 176 // comment in the AudioHardware.h for constant | 177 // comment in the AudioHardware.h for constant |
| 177 // kAudioDevicePropertyDeviceUID. | 178 // kAudioDevicePropertyDeviceUID. |
| 178 if (uid) | 179 if (uid) |
| 179 CFRelease(uid); | 180 CFRelease(uid); |
| 180 if (name) | 181 if (name) |
| 181 CFRelease(name); | 182 CFRelease(name); |
| 182 } | 183 } |
| 183 } | 184 } |
| 184 | 185 |
| 186 static AudioDeviceID GetAudioDeviceIdByUId(bool is_input, |
| 187 const std::string& device_uid) { |
| 188 AudioObjectPropertyAddress property_address = { |
| 189 kAudioHardwarePropertyDevices, |
| 190 kAudioObjectPropertyScopeGlobal, |
| 191 kAudioObjectPropertyElementMaster |
| 192 }; |
| 193 AudioDeviceID device_id = kAudioObjectUnknown; |
| 194 UInt32 device_size = sizeof(device_id); |
| 195 OSStatus result = -1; |
| 196 |
| 197 if ((device_uid == AudioManagerBase::kDefaultDeviceId) || |
| 198 device_uid.empty()) { |
| 199 // Default Device. |
| 200 if (is_input) |
| 201 property_address.mSelector = kAudioHardwarePropertyDefaultInputDevice; |
| 202 else |
| 203 property_address.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 204 |
| 205 result = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 206 &property_address, |
| 207 0, |
| 208 0, |
| 209 &device_size, |
| 210 &device_id); |
| 211 } else { // Non-default device. |
| 212 base::mac::ScopedCFTypeRef<CFStringRef> |
| 213 uid(base::SysUTF8ToCFStringRef(device_uid)); |
| 214 AudioValueTranslation value; |
| 215 value.mInputData = &uid; |
| 216 value.mInputDataSize = sizeof(CFStringRef); |
| 217 value.mOutputData = &device_id; |
| 218 value.mOutputDataSize = device_size; |
| 219 UInt32 translation_size = sizeof(AudioValueTranslation); |
| 220 |
| 221 property_address.mSelector = kAudioHardwarePropertyDeviceForUID; |
| 222 result = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 223 &property_address, |
| 224 0, //translation_size, |
| 225 0, //&value, |
| 226 &translation_size, |
| 227 &value); |
| 228 } |
| 229 |
| 230 if (result) |
| 231 DLOG(WARNING) << "Unable to query device " << device_uid |
| 232 << " for AudioDeviceID "; |
| 233 |
| 234 return device_id; |
| 235 } |
| 236 |
| 185 AudioManagerMac::AudioManagerMac() | 237 AudioManagerMac::AudioManagerMac() |
| 186 : num_output_streams_(0) { | 238 : num_output_streams_(0) { |
| 187 } | 239 } |
| 188 | 240 |
| 189 AudioManagerMac::~AudioManagerMac() { | 241 AudioManagerMac::~AudioManagerMac() { |
| 190 } | 242 } |
| 191 | 243 |
| 192 bool AudioManagerMac::HasAudioOutputDevices() { | 244 bool AudioManagerMac::HasAudioOutputDevices() { |
| 193 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); | 245 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); |
| 194 } | 246 } |
| 195 | 247 |
| 196 bool AudioManagerMac::HasAudioInputDevices() { | 248 bool AudioManagerMac::HasAudioInputDevices() { |
| 197 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); | 249 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); |
| 198 } | 250 } |
| 199 | 251 |
| 200 void AudioManagerMac::GetAudioInputDeviceNames( | 252 void AudioManagerMac::GetAudioInputDeviceNames( |
| 201 media::AudioDeviceNames* device_names) { | 253 media::AudioDeviceNames* device_names) { |
| 202 // This is needed because AudioObjectGetPropertyDataSize has memory leak | 254 // This is needed because AudioObjectGetPropertyDataSize has memory leak |
| 203 // when there is no soundcard in the machine. | 255 // when there is no soundcard in the machine. |
| 204 if (!HasAudioInputDevices()) | 256 if (!HasAudioInputDevices()) |
| 205 return; | 257 return; |
| 206 | 258 |
| 207 GetAudioDeviceInfo(true, device_names); | 259 GetAudioDeviceInfo(true, device_names); |
| 208 if (!device_names->empty()) { | 260 if (!device_names->empty()) { |
| 209 // Prepend the default device to the list since we always want it to be | 261 // Prepend the default device to the list since we always want it to be |
| 210 // on the top of the list for all platforms. There is no duplicate | 262 // on the top of the list for all platforms. There is no duplicate |
| 211 // counting here since the default device has been abstracted out before. | 263 // counting here since the default device has been abstracted out before. |
| 212 media::AudioDeviceName name; | 264 media::AudioDeviceName name; |
| 213 name.device_name = AudioManagerBase::kDefaultDeviceName; | 265 name.device_name = AudioManagerBase::kDefaultDeviceName; |
| 214 name.unique_id = "0"; | 266 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
| 215 device_names->push_front(name); | 267 device_names->push_front(name); |
| 216 } | 268 } |
| 217 } | 269 } |
| 218 | 270 |
| 219 AudioOutputStream* AudioManagerMac::MakeAudioOutputStream( | 271 AudioOutputStream* AudioManagerMac::MakeAudioOutputStream( |
| 220 const AudioParameters& params) { | 272 const AudioParameters& params) { |
| 221 if (!params.IsValid()) | 273 if (!params.IsValid()) |
| 222 return NULL; | 274 return NULL; |
| 223 | 275 |
| 224 // Limit the number of audio streams opened. This is to prevent using | 276 // Limit the number of audio streams opened. This is to prevent using |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 num_output_streams_++; | 287 num_output_streams_++; |
| 236 return new PCMQueueOutAudioOutputStream(this, params); | 288 return new PCMQueueOutAudioOutputStream(this, params); |
| 237 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | 289 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 238 num_output_streams_++; | 290 num_output_streams_++; |
| 239 return new AUAudioOutputStream(this, params); | 291 return new AUAudioOutputStream(this, params); |
| 240 } | 292 } |
| 241 return NULL; | 293 return NULL; |
| 242 } | 294 } |
| 243 | 295 |
| 244 AudioInputStream* AudioManagerMac::MakeAudioInputStream( | 296 AudioInputStream* AudioManagerMac::MakeAudioInputStream( |
| 245 const AudioParameters& params) { | 297 const AudioParameters& params, const std::string& device_uid) { |
| 246 if (!params.IsValid() || (params.channels > kMaxInputChannels)) | 298 if (!params.IsValid() || (params.channels > kMaxInputChannels)) |
| 247 return NULL; | 299 return NULL; |
| 248 | 300 |
| 249 if (params.format == AudioParameters::AUDIO_MOCK) { | 301 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 250 return FakeAudioInputStream::MakeFakeStream(params); | 302 return FakeAudioInputStream::MakeFakeStream(params); |
| 251 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | 303 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { |
| 252 return new PCMQueueInAudioInputStream(this, params); | 304 return new PCMQueueInAudioInputStream(this, params); |
| 253 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | 305 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 254 return new AUAudioInputStream(this, params); | 306 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_uid); |
| 307 if (audio_device_id != kAudioObjectUnknown) |
| 308 return new AUAudioInputStream(this, params, audio_device_id); |
| 255 } | 309 } |
| 256 return NULL; | 310 return NULL; |
| 257 } | 311 } |
| 258 | 312 |
| 259 void AudioManagerMac::MuteAll() { | 313 void AudioManagerMac::MuteAll() { |
| 260 // TODO(cpu): implement. | 314 // TODO(cpu): implement. |
| 261 } | 315 } |
| 262 | 316 |
| 263 void AudioManagerMac::UnMuteAll() { | 317 void AudioManagerMac::UnMuteAll() { |
| 264 // TODO(cpu): implement. | 318 // TODO(cpu): implement. |
| 265 } | 319 } |
| 266 | 320 |
| 267 // Called by the stream when it has been released by calling Close(). | 321 // Called by the stream when it has been released by calling Close(). |
| 268 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { | 322 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { |
| 269 DCHECK(stream); | 323 DCHECK(stream); |
| 270 num_output_streams_--; | 324 num_output_streams_--; |
| 271 delete stream; | 325 delete stream; |
| 272 } | 326 } |
| 273 | 327 |
| 274 // Called by the stream when it has been released by calling Close(). | 328 // Called by the stream when it has been released by calling Close(). |
| 275 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { | 329 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { |
| 276 delete stream; | 330 delete stream; |
| 277 } | 331 } |
| 278 | 332 |
| 279 // static | 333 // static |
| 280 AudioManager* AudioManager::CreateAudioManager() { | 334 AudioManager* AudioManager::CreateAudioManager() { |
| 281 return new AudioManagerMac(); | 335 return new AudioManagerMac(); |
| 282 } | 336 } |
| OLD | NEW |