OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/audio/win/core_audio_util_win.h" | |
6 | |
7 #include <Audioclient.h> | |
8 #include <Functiondiscoverykeys_devpkey.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "base/win/scoped_co_mem.h" | |
14 #include "base/win/windows_version.h" | |
15 | |
16 using base::win::ScopedCoMem; | |
17 | |
18 namespace media { | |
19 | |
20 // Scoped PROPVARIANT class for automatically freeing a COM PROPVARIANT | |
21 // structure at the end of a scope. | |
22 class ScopedPropertyVariant { | |
23 public: | |
24 ScopedPropertyVariant() { | |
25 PropVariantInit(&propvar_); | |
26 } | |
27 ~ScopedPropertyVariant() { | |
28 PropVariantClear(&propvar_); | |
29 } | |
30 | |
31 // Retrieves the pointer address. | |
32 // Used to receive a PROPVARIANT as an out argument (and take ownership). | |
33 PROPVARIANT* Receive() { | |
34 DCHECK_EQ(propvar_.vt, VT_EMPTY); | |
35 return &propvar_; | |
36 } | |
37 | |
38 VARTYPE type() const { | |
39 return propvar_.vt; | |
40 } | |
41 | |
42 LPWSTR as_wide_string() const { | |
43 DCHECK_EQ(type(), VT_LPWSTR); | |
44 return propvar_.pwszVal; | |
45 } | |
46 | |
47 private: | |
48 PROPVARIANT propvar_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(ScopedPropertyVariant); | |
51 }; | |
52 | |
53 bool CoreAudioUtil::IsSupported() { | |
54 // Microsoft does not plan to make the Core Audio APIs available for use | |
55 // with earlier versions of Windows, including Microsoft Windows Server 2003, | |
56 // Windows XP, Windows Millennium Edition, Windows 2000, and Windows 98. | |
57 return (base::win::GetVersion() >= base::win::VERSION_VISTA); | |
58 } | |
59 | |
60 int CoreAudioUtil::NumberOfActiveDevices(EDataFlow data_flow) { | |
61 DCHECK(CoreAudioUtil::IsSupported()); | |
62 // Create the IMMDeviceEnumerator interface. | |
63 ScopedComPtr<IMMDeviceEnumerator> device_enumerator = | |
64 CreateDeviceEnumerator(); | |
65 if (!device_enumerator) | |
66 return 0; | |
67 | |
68 // Generate a collection of active (present and not disabled) audio endpoint | |
69 // devices for the specified data-flow direction. | |
70 // This method will succeed even if all devices are disabled. | |
71 ScopedComPtr<IMMDeviceCollection> collection; | |
72 HRESULT hr = device_enumerator->EnumAudioEndpoints(data_flow, | |
73 DEVICE_STATE_ACTIVE, | |
74 collection.Receive()); | |
75 if (FAILED(hr)) { | |
76 LOG(ERROR) << "IMMDeviceCollection::EnumAudioEndpoints: " << std::hex << hr; | |
77 return 0; | |
78 } | |
79 | |
80 // Retrieve the number of active audio devices for the specified direction | |
81 UINT number_of_active_devices = 0; | |
82 collection->GetCount(&number_of_active_devices); | |
83 DVLOG(1) << ((data_flow == eCapture) ? "[in ] " : "[out] ") | |
84 << "number of devices: " << number_of_active_devices; | |
85 return static_cast<int>(number_of_active_devices); | |
86 } | |
87 | |
88 ScopedComPtr<IMMDeviceEnumerator> CoreAudioUtil::CreateDeviceEnumerator() { | |
89 DCHECK(CoreAudioUtil::IsSupported()); | |
90 ScopedComPtr<IMMDeviceEnumerator> device_enumerator; | |
91 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), | |
92 NULL, | |
93 CLSCTX_INPROC_SERVER, | |
94 __uuidof(IMMDeviceEnumerator), | |
95 device_enumerator.ReceiveVoid()); | |
96 // CO_E_NOTINITIALIZED is the most likely reason for failure and if that | |
97 // happens we might as well die here. | |
98 CHECK(SUCCEEDED(hr)); | |
99 return device_enumerator; | |
100 } | |
101 | |
102 ScopedComPtr<IMMDevice> CoreAudioUtil::CreateDefaultDevice(EDataFlow data_flow, | |
103 ERole role) { | |
104 DCHECK(CoreAudioUtil::IsSupported()); | |
105 ScopedComPtr<IMMDevice> endpoint_device; | |
106 | |
107 // Create the IMMDeviceEnumerator interface. | |
108 ScopedComPtr<IMMDeviceEnumerator> device_enumerator = | |
109 CreateDeviceEnumerator(); | |
110 if (!device_enumerator) | |
111 return endpoint_device; | |
112 | |
113 // Retrieve the default audio endpoint for the specified data-flow | |
114 // direction and role. | |
115 HRESULT hr = device_enumerator->GetDefaultAudioEndpoint( | |
116 data_flow, role, endpoint_device.Receive()); | |
117 | |
118 if (FAILED(hr)) { | |
119 DVLOG(1) << "IMMDeviceEnumerator::GetDefaultAudioEndpoint: " | |
120 << std::hex << hr; | |
121 return endpoint_device; | |
122 } | |
123 | |
124 // Verify that the audio endpoint device is active, i.e., that the audio | |
125 // adapter that connects to the endpoint device is present and enabled. | |
126 DWORD state = DEVICE_STATE_DISABLED; | |
127 hr = endpoint_device->GetState(&state); | |
128 if (SUCCEEDED(hr)) { | |
129 if (!(state & DEVICE_STATE_ACTIVE)) { | |
130 DVLOG(1) << "Selected endpoint device is not active"; | |
131 endpoint_device.Release(); | |
132 } | |
133 } | |
134 return endpoint_device; | |
135 } | |
136 | |
137 ScopedComPtr<IMMDevice> CoreAudioUtil::CreateDevice( | |
138 const std::string& device_id) { | |
139 DCHECK(CoreAudioUtil::IsSupported()); | |
140 ScopedComPtr<IMMDevice> endpoint_device; | |
141 | |
142 // Create the IMMDeviceEnumerator interface. | |
143 ScopedComPtr<IMMDeviceEnumerator> device_enumerator = | |
144 CreateDeviceEnumerator(); | |
145 if (!device_enumerator) | |
146 return endpoint_device; | |
147 | |
148 // Retrieve an audio device specified by an endpoint device-identification | |
149 // string. | |
150 HRESULT hr = device_enumerator->GetDevice(UTF8ToUTF16(device_id).c_str(), | |
151 endpoint_device.Receive()); | |
152 DVLOG_IF(1, FAILED(hr)) << "IMMDeviceEnumerator::GetDevice: " | |
153 << std::hex << hr; | |
154 return endpoint_device; | |
155 } | |
156 | |
157 HRESULT CoreAudioUtil::GetDeviceName(IMMDevice* device, AudioDeviceName* name) { | |
158 DCHECK(CoreAudioUtil::IsSupported()); | |
159 DCHECK(device); | |
160 AudioDeviceName device_name; | |
161 | |
162 // Retrieve unique name of endpoint device. | |
163 // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}". | |
164 ScopedCoMem<WCHAR> endpoint_device_id; | |
165 HRESULT hr = device->GetId(&endpoint_device_id); | |
166 if (FAILED(hr)) | |
167 return hr; | |
168 WideToUTF8(endpoint_device_id, wcslen(endpoint_device_id), | |
169 &device_name.unique_id); | |
170 | |
171 // Retrieve user-friendly name of endpoint device. | |
172 // Example: "Microphone (Realtek High Definition Audio)". | |
173 ScopedComPtr<IPropertyStore> properties; | |
174 hr = device->OpenPropertyStore(STGM_READ, properties.Receive()); | |
175 if (FAILED(hr)) | |
176 return hr; | |
177 ScopedPropertyVariant friendly_name; | |
178 hr = properties->GetValue(PKEY_Device_FriendlyName, friendly_name.Receive()); | |
179 if (FAILED(hr)) | |
180 return hr; | |
181 if (friendly_name.as_wide_string()) | |
tommi (sloooow) - chröme
2012/10/29 12:20:25
nit: {}
henrika (OOO until Aug 14)
2012/10/29 12:34:33
Done.
| |
182 WideToUTF8(friendly_name.as_wide_string(), | |
183 wcslen(friendly_name.as_wide_string()), | |
184 &device_name.device_name); | |
185 | |
186 *name = device_name; | |
187 DVLOG(1) << "friendly name: " << device_name.device_name; | |
188 DVLOG(1) << "unique id : " << device_name.unique_id; | |
189 return hr; | |
190 } | |
191 | |
192 std::string CoreAudioUtil::GetFriendlyName(const std::string& device_id) { | |
193 DCHECK(CoreAudioUtil::IsSupported()); | |
194 | |
195 ScopedComPtr<IMMDevice> audio_device = CreateDevice(device_id); | |
196 if (!audio_device) | |
197 return std::string(); | |
198 | |
199 AudioDeviceName device_name; | |
200 HRESULT hr = GetDeviceName(audio_device, &device_name); | |
201 if (FAILED(hr)) | |
202 return std::string(); | |
203 | |
204 return device_name.device_name; | |
205 } | |
206 | |
207 bool CoreAudioUtil::DeviceIsDefault(EDataFlow flow, | |
208 ERole role, | |
209 std::string device_id) { | |
210 DCHECK(CoreAudioUtil::IsSupported()); | |
211 ScopedComPtr<IMMDevice> device = CreateDefaultDevice(flow, role); | |
212 if (!device) | |
213 return false; | |
214 | |
215 ScopedCoMem<WCHAR> default_device_id; | |
216 HRESULT hr = device->GetId(&default_device_id); | |
217 if (FAILED(hr)) | |
218 return false; | |
219 | |
220 std::string str_default; | |
221 WideToUTF8(default_device_id, wcslen(default_device_id), &str_default); | |
222 if (device_id.compare(str_default) != 0) | |
223 return false; | |
224 return true; | |
225 } | |
226 | |
227 EDataFlow CoreAudioUtil::GetDataFlow(IMMDevice* device) { | |
228 DCHECK(CoreAudioUtil::IsSupported()); | |
229 DCHECK(device); | |
230 | |
231 ScopedComPtr<IMMEndpoint> endpoint; | |
232 HRESULT hr = device->QueryInterface(endpoint.Receive()); | |
233 if (FAILED(hr)) { | |
234 DVLOG(1) << "IMMDevice::QueryInterface: " << std::hex << hr; | |
235 return eAll; | |
236 } | |
237 | |
238 EDataFlow data_flow; | |
239 hr = endpoint->GetDataFlow(&data_flow); | |
240 if (FAILED(hr)) { | |
241 DVLOG(1) << "IMMEndpoint::GetDataFlow: " << std::hex << hr; | |
242 return eAll; | |
243 } | |
244 return data_flow; | |
245 } | |
246 | |
247 ScopedComPtr<IAudioClient> CoreAudioUtil::CreateClient( | |
248 IMMDevice* audio_device) { | |
249 DCHECK(CoreAudioUtil::IsSupported()); | |
250 ScopedComPtr<IAudioClient> audio_client; | |
251 | |
252 // Creates and activates an IAudioClient COM object given the selected | |
253 // endpoint device. | |
254 HRESULT hr = audio_device->Activate(__uuidof(IAudioClient), | |
255 CLSCTX_INPROC_SERVER, | |
256 NULL, | |
257 audio_client.ReceiveVoid()); | |
258 DVLOG_IF(1, FAILED(hr)) << "IMMDevice::Activate: " << std::hex << hr; | |
259 return audio_client; | |
260 } | |
261 | |
262 } // namespace media | |
OLD | NEW |