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/sys_string_conversions.h" | |
8 #include "media/audio/fake_audio_input_stream.h" | 9 #include "media/audio/fake_audio_input_stream.h" |
9 #include "media/audio/fake_audio_output_stream.h" | 10 #include "media/audio/fake_audio_output_stream.h" |
10 #include "media/audio/mac/audio_input_mac.h" | 11 #include "media/audio/mac/audio_input_mac.h" |
11 #include "media/audio/mac/audio_low_latency_input_mac.h" | 12 #include "media/audio/mac/audio_low_latency_input_mac.h" |
12 #include "media/audio/mac/audio_low_latency_output_mac.h" | 13 #include "media/audio/mac/audio_low_latency_output_mac.h" |
13 #include "media/audio/mac/audio_manager_mac.h" | 14 #include "media/audio/mac/audio_manager_mac.h" |
14 #include "media/audio/mac/audio_output_mac.h" | 15 #include "media/audio/mac/audio_output_mac.h" |
15 #include "media/base/limits.h" | 16 #include "media/base/limits.h" |
16 | 17 |
17 static const int kMaxInputChannels = 2; | 18 static const int kMaxInputChannels = 2; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, | 59 OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
59 &property_address, | 60 &property_address, |
60 0, // inQualifierDataSize | 61 0, // inQualifierDataSize |
61 NULL, // inQualifierData | 62 NULL, // inQualifierData |
62 &output_device_id_size, | 63 &output_device_id_size, |
63 &output_device_id); | 64 &output_device_id); |
64 return err == kAudioHardwareNoError && | 65 return err == kAudioHardwareNoError && |
65 output_device_id != kAudioObjectUnknown; | 66 output_device_id != kAudioObjectUnknown; |
66 } | 67 } |
67 | 68 |
69 static void GetAudioDeviceInfo(bool is_input, | |
70 media::AudioDeviceNames* device_names) { | |
71 if (!device_names) | |
scherkus (not reviewing)
2011/10/19 18:19:26
is this valid? seems like programmer error -- I'd
no longer working on chromium
2011/10/21 12:35:30
Agree, let me just put a DCHECK here.
| |
72 return; | |
73 | |
74 device_names->clear(); | |
75 | |
76 // Query the number of total devices. | |
77 AudioObjectPropertyAddress property_address = { | |
78 kAudioHardwarePropertyDevices, // mSelector | |
79 kAudioObjectPropertyScopeGlobal, // mScope | |
80 kAudioObjectPropertyElementMaster // mElement | |
81 }; | |
82 UInt32 size = 0; | |
83 OSStatus result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, | |
84 &property_address, | |
85 0, | |
86 NULL, | |
87 &size); | |
88 if (result || !size) | |
89 return; | |
90 | |
91 int device_count = 0; | |
92 device_count = size / sizeof(AudioDeviceID); | |
93 | |
94 // Get the array of device id for each device. | |
95 scoped_ptr_malloc<AudioDeviceID> | |
96 devices(reinterpret_cast<AudioDeviceID*>(malloc(size))); | |
97 AudioDeviceID* device_ids = devices.get(); | |
98 result = AudioObjectGetPropertyData(kAudioObjectSystemObject, | |
99 &property_address, | |
100 0, | |
101 NULL, | |
102 &size, | |
103 device_ids); | |
104 if (result) | |
105 return; | |
106 | |
107 // Iterate each device to gather information. | |
108 for (int i = 0; i < device_count; ++i) { | |
109 { | |
110 int channels = 0; | |
111 // Get the number of input or output channels of the device, exclude | |
112 // those devices without any right type of channel. | |
113 AudioDeviceGetPropertyInfo(device_ids[i], | |
114 0, | |
115 is_input, | |
116 kAudioDevicePropertyStreamConfiguration, | |
117 &size, | |
118 NULL); | |
119 scoped_ptr_malloc<AudioBufferList> | |
120 buffer(reinterpret_cast<AudioBufferList*>(malloc(size))); | |
scherkus (not reviewing)
2011/10/19 18:19:26
maybe I'm missing something -- but doesn't the for
no longer working on chromium
2011/10/21 12:35:30
True, I was quite tired at that late evening that
| |
121 AudioBufferList* buffer_list = buffer.get(); | |
122 AudioDeviceGetProperty(device_ids[i], | |
123 0, | |
124 is_input, | |
125 kAudioDevicePropertyStreamConfiguration, | |
126 &size, | |
127 buffer_list); | |
128 | |
129 for (uint32 j = 0; j < buffer_list->mNumberBuffers; ++j) | |
130 channels += buffer_list->mBuffers[j].mNumberChannels; | |
131 | |
132 if (!channels) | |
133 continue; | |
134 } | |
135 | |
136 // Get device UID. | |
137 CFStringRef uid = NULL; | |
138 size = sizeof(uid); | |
139 result = AudioDeviceGetProperty(device_ids[i], | |
140 0, | |
141 is_input, | |
142 kAudioDevicePropertyDeviceUID, | |
143 &size, | |
144 &uid); | |
145 if (result) | |
146 continue; | |
147 | |
148 // Get device name. | |
149 CFStringRef name = NULL; | |
150 result = AudioDeviceGetProperty(device_ids[i], | |
151 0, | |
152 is_input, | |
153 kAudioObjectPropertyName, | |
154 &size, | |
155 &name); | |
156 if (result) | |
157 continue; | |
158 | |
159 media::AudioDeviceName device_name; | |
160 device_name.device_name = base::SysCFStringRefToUTF8(name); | |
161 device_name.unique_id = base::SysCFStringRefToUTF8(uid); | |
162 device_names->push_back(device_name); | |
163 } | |
164 } | |
165 | |
68 AudioManagerMac::AudioManagerMac() | 166 AudioManagerMac::AudioManagerMac() |
69 : num_output_streams_(0) { | 167 : num_output_streams_(0) { |
70 } | 168 } |
71 | 169 |
72 AudioManagerMac::~AudioManagerMac() { | 170 AudioManagerMac::~AudioManagerMac() { |
73 } | 171 } |
74 | 172 |
75 bool AudioManagerMac::HasAudioOutputDevices() { | 173 bool AudioManagerMac::HasAudioOutputDevices() { |
76 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); | 174 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); |
77 } | 175 } |
78 | 176 |
79 bool AudioManagerMac::HasAudioInputDevices() { | 177 bool AudioManagerMac::HasAudioInputDevices() { |
80 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); | 178 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); |
81 } | 179 } |
82 | 180 |
83 void AudioManagerMac::GetAudioInputDeviceNames( | 181 void AudioManagerMac::GetAudioInputDeviceNames( |
84 media::AudioDeviceNames* device_names) { | 182 media::AudioDeviceNames* device_names) { |
85 // TODO(xians): query a full list of valid devices. | 183 GetAudioDeviceInfo(true, device_names); |
86 if (HasAudioInputDevices()) { | 184 if (!device_names->empty()) { |
87 // Add the default device to the list. | 185 // Prepend the default device to the list since we always want it to be |
88 // We use index 0 to make up the unique_id to identify the | 186 // on the top of the list for all platforms. There is no duplicate |
89 // default devices. | 187 // counting here since the default device has been abstracted out before. |
90 media::AudioDeviceName name; | 188 media::AudioDeviceName name; |
91 name.device_name = AudioManagerBase::kDefaultDeviceName; | 189 name.device_name = AudioManagerBase::kDefaultDeviceName; |
92 name.unique_id = "0"; | 190 name.unique_id = "0"; |
93 device_names->push_back(name); | 191 device_names->push_front(name); |
94 } | 192 } |
95 } | 193 } |
96 | 194 |
97 AudioOutputStream* AudioManagerMac::MakeAudioOutputStream( | 195 AudioOutputStream* AudioManagerMac::MakeAudioOutputStream( |
98 const AudioParameters& params) { | 196 const AudioParameters& params) { |
99 if (!params.IsValid()) | 197 if (!params.IsValid()) |
100 return NULL; | 198 return NULL; |
101 | 199 |
102 // Limit the number of audio streams opened. This is to prevent using | 200 // Limit the number of audio streams opened. This is to prevent using |
103 // excessive resources for a large number of audio streams. More | 201 // excessive resources for a large number of audio streams. More |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 | 249 |
152 // Called by the stream when it has been released by calling Close(). | 250 // Called by the stream when it has been released by calling Close(). |
153 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { | 251 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { |
154 delete stream; | 252 delete stream; |
155 } | 253 } |
156 | 254 |
157 // static | 255 // static |
158 AudioManager* AudioManager::CreateAudioManager() { | 256 AudioManager* AudioManager::CreateAudioManager() { |
159 return new AudioManagerMac(); | 257 return new AudioManagerMac(); |
160 } | 258 } |
OLD | NEW |