Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: media/audio/win/core_audio_util_win.cc

Issue 11226057: Adds Core Audio Utility methods for Windows (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Improved comments Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #define PRINT(sz, ...) wprintf(sz L"\n", __VA_ARGS__);
scherkus (not reviewing) 2012/10/24 16:48:04 I'm not seeing this used -- time to remove?
henrika (OOO until Aug 14) 2012/10/24 19:25:44 Correct. Used while developing.
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
21
22 class ScopedPropertyVariant {
DaleCurtis 2012/10/25 01:08:08 Is this a pattern that other people would benefit
tommi (sloooow) - chröme 2012/10/25 08:42:11 PROPVARIANT isn't that widely used so having it he
henrika (OOO until Aug 14) 2012/10/26 17:51:56 I added it when I did enumeration for Dale's IMMNo
23 public:
24 ScopedPropertyVariant() {
25 PropVariantInit(&prop_);
26 }
27 ~ScopedPropertyVariant() {
28 PropVariantClear(&prop_);
29 }
30
31 PROPVARIANT* Get() {
32 return &prop_;
33 }
34
35 private:
36 PROPVARIANT prop_;
37
38 DISALLOW_COPY_AND_ASSIGN(ScopedPropertyVariant);
39 };
40
41 bool CoreAudioIsSupported() {
42 // Microsoft does not plan to make the Core Audio APIs available for use
43 // with earlier versions of Windows, including Microsoft Windows Server 2003,
44 // Windows XP, Windows Millennium Edition, Windows 2000, and Windows 98.
45 return (base::win::GetVersion() >= base::win::VERSION_VISTA);
scherkus (not reviewing) 2012/10/24 16:48:04 as I noted in dalecurtis' http://codereview.chromi
henrika (OOO until Aug 14) 2012/10/24 19:25:44 I can make a follow-up patch when this one has lan
DaleCurtis 2012/10/25 01:08:08 Is core audio always supported when this is true?
henrika (OOO until Aug 14) 2012/10/26 17:51:56 I don't have a better method. You could also do so
46 }
47
48 int CoreAudioNumberOfActiveDevices(EDataFlow data_flow) {
49 DCHECK(CoreAudioIsSupported());
50 // Create the IMMDeviceEnumerator interface.
51 ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
52 CoreAudioCreateDeviceEnumerator();
53 if (!device_enumerator)
54 return 0;
55
56 // Generate a collection of active (present and not disabled) audio endpoint
57 // devices for the specified data-flow direction.
58 // This method will succeed even if all devices are disabled.
59 ScopedComPtr<IMMDeviceCollection> collection;
60 HRESULT hr = device_enumerator->EnumAudioEndpoints(data_flow,
61 DEVICE_STATE_ACTIVE,
62 collection.Receive());
63 if (FAILED(hr)) {
64 LOG(ERROR) << "IMMDeviceCollection::EnumAudioEndpoints: " << std::hex << hr;
65 return 0;
66 }
67
68 // Retrieve the number of active audio devices for the specified direction
69 UINT number_of_active_devices = 0;
70 collection->GetCount(&number_of_active_devices);
71 std::string flow = (data_flow == eCapture) ? "[in ] " : "[out] ";
tommi (sloooow) - chröme 2012/10/25 08:42:11 if this is only for debugging, keep it in an #ifnd
72 DVLOG(1) << flow << "number of devices: " << number_of_active_devices;
73 return static_cast<int>(number_of_active_devices);
74 }
75
76 ScopedComPtr<IMMDeviceEnumerator> CoreAudioCreateDeviceEnumerator() {
77 DCHECK(CoreAudioIsSupported());
78 ScopedComPtr<IMMDeviceEnumerator> device_enumerator;
79 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
80 NULL,
81 CLSCTX_INPROC_SERVER,
82 __uuidof(IMMDeviceEnumerator),
83 device_enumerator.ReceiveVoid());
84 LOG_IF(ERROR, FAILED(hr)) << "CoCreateInstance(IMMDeviceEnumerator): "
tommi (sloooow) - chröme 2012/10/25 08:42:11 I'm tempted to make this a CHECK(SUCCEEDED(hr)).
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Agree. We should actually.
85 << std::hex << hr;
86 return device_enumerator;
87 }
88
89 ScopedComPtr<IMMDevice> CoreAudioCreateDefaultDevice(EDataFlow data_flow,
90 ERole role) {
91 DCHECK(CoreAudioIsSupported());
92 ScopedComPtr<IMMDevice> endpoint_device;
93
94 // Create the IMMDeviceEnumerator interface.
95 ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
96 CoreAudioCreateDeviceEnumerator();
97 if (!device_enumerator)
98 return endpoint_device;
99
100 // Retrieve the default audio endpoint for the specified data-flow
101 // direction and role.
102 HRESULT hr = device_enumerator->GetDefaultAudioEndpoint(
103 data_flow, role, endpoint_device.Receive());
104
105 if (FAILED(hr)) {
106 LOG(ERROR) << "IMMDeviceEnumerator::GetDefaultAudioEndpoint: "
tommi (sloooow) - chröme 2012/10/25 08:42:11 DVLOG. There's a lot of logging in this file and
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
107 << std::hex << hr;
108 return endpoint_device;
109 }
110
111 // Verify that the audio endpoint device is active, i.e., that the audio
112 // adapter that connects to the endpoint device is present and enabled.
113 DWORD state = DEVICE_STATE_DISABLED;
114 hr = endpoint_device->GetState(&state);
115 if (SUCCEEDED(hr)) {
116 if (!(state & DEVICE_STATE_ACTIVE)) {
117 LOG(ERROR) << "Selected endpoint device is not active";
118 endpoint_device.Release();
119 }
120 }
121 return endpoint_device;
122 }
123
124 ScopedComPtr<IMMDevice> CoreAudioCreateDevice(const std::string& device_id) {
125 DCHECK(CoreAudioIsSupported());
126 ScopedComPtr<IMMDevice> endpoint_device;
127
128 // Create the IMMDeviceEnumerator interface.
129 ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
130 CoreAudioCreateDeviceEnumerator();
131 if (!device_enumerator)
132 return endpoint_device;
133
134 // Retrieve an audio device specified by an endpoint device-identification
135 // string.
136 HRESULT hr = device_enumerator->GetDevice(UTF8ToUTF16(device_id).c_str(),
137 endpoint_device.Receive());
138 LOG_IF(ERROR, FAILED(hr)) << "IMMDeviceEnumerator::GetDevice: "
139 << std::hex << hr;
140 return endpoint_device;
141 }
142
143 HRESULT CoreAudioGetDeviceName(IMMDevice* device, AudioDeviceName* name) {
144 DCHECK(CoreAudioIsSupported());
145 DCHECK(device);
146 AudioDeviceName device_name;
147
148 // Retrieve unique name of endpoint device.
149 // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}".
150 ScopedCoMem<WCHAR> endpoint_device_id;
151 HRESULT hr = device->GetId(&endpoint_device_id);
152 if (FAILED(hr))
153 return hr;
154 device_name.unique_id = WideToUTF8(static_cast<WCHAR*>(endpoint_device_id));
tommi (sloooow) - chröme 2012/10/25 08:42:11 is the static_cast needed?
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Changed to WideToUTF8(endpoint_device_id, wcslen(e
155
156 // Retrieve user-friendly name of endpoint device.
157 // Example: "Microphone (Realtek High Definition Audio)".
158 ScopedComPtr<IPropertyStore> properties;
159 hr = device->OpenPropertyStore(STGM_READ, properties.Receive());
160 if (FAILED(hr))
161 return hr;
162 PROPVARIANT friendly_name;
tommi (sloooow) - chröme 2012/10/25 08:42:11 why not use the ScopedPropVariant class?
henrika (OOO until Aug 14) 2012/10/26 17:51:56 LOL ;-) I used it for debugging only. Forgot I co
163 PropVariantInit(&friendly_name);
164 hr = properties->GetValue(PKEY_Device_FriendlyName, &friendly_name);
165 if (FAILED(hr))
166 return hr;
167 if (friendly_name.vt == VT_LPWSTR && friendly_name.pwszVal) {
tommi (sloooow) - chröme 2012/10/25 08:42:11 no {} (as is done in the if() right above)
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
168 device_name.device_name = WideToUTF8(friendly_name.pwszVal);
169 }
170 PropVariantClear(&friendly_name);
171
172 *name = device_name;
173 DVLOG(1) << "friendly name: " << device_name.device_name;
174 DVLOG(1) << "unique id : " << device_name.unique_id;
175 return hr;
176 }
177
178 std::string CoreAudioGetFriendlyName(const std::string& device_id) {
179 DCHECK(CoreAudioIsSupported());
180 std::string name;
181
182 ScopedComPtr<IMMDevice> audio_device = CoreAudioCreateDevice(device_id);
183 if (!audio_device)
184 return name;
185
186 AudioDeviceName device_name;
187 HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name);
188 if (FAILED(hr))
189 return name;
190
191 name = device_name.device_name;
192 return name;
tommi (sloooow) - chröme 2012/10/25 08:42:11 nit: you don't really need the name variable. coul
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
193 }
194
195 bool CoreAudioDeviceIsDefault(EDataFlow flow,
196 ERole role,
197 std::string device_id) {
198 DCHECK(CoreAudioIsSupported());
199 ScopedComPtr<IMMDevice> device = CoreAudioCreateDefaultDevice(flow, role);
200 if (!device)
201 return false;
202
203 ScopedCoMem<WCHAR> default_device_id;
204 HRESULT hr = device->GetId(&default_device_id);
205 if (FAILED(hr))
206 return false;
207
208 std::string str_default = WideToUTF8(static_cast<WCHAR*>(default_device_id));
tommi (sloooow) - chröme 2012/10/25 08:42:11 static_cast?
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Fixed as above.
209 if (device_id.compare(str_default) != 0) {
tommi (sloooow) - chröme 2012/10/25 08:42:11 no {}
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
210 return false;
211 }
212 return true;
213 }
214
215 EDataFlow CoreAudioGetDataFlow(IMMDevice* device) {
216 DCHECK(CoreAudioIsSupported());
217 if (!device)
tommi (sloooow) - chröme 2012/10/25 08:42:11 should we instead DCHECK(device) here and assume t
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
218 return eAll;
219
220 ScopedComPtr<IMMEndpoint> endpoint;
221 device->QueryInterface(endpoint.Receive());
tommi (sloooow) - chröme 2012/10/25 08:42:11 I have a feeling that this might fail unless it's
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
222
223 EDataFlow data_flow;
224 HRESULT hr = endpoint->GetDataFlow(&data_flow);
225 if (FAILED(hr)) {
226 LOG(ERROR) << "IMMEndpoint::GetDataFlow: " << std::hex << hr;
227 return eAll;
228 }
229 return data_flow;
230 }
231
232 ScopedComPtr<IAudioClient> CoreAudioCreateClient(IMMDevice* audio_device) {
233 DCHECK(CoreAudioIsSupported());
234 ScopedComPtr<IAudioClient> audio_client;
235
236 // Creates and activates an IAudioClient COM object given the selected
237 // endpoint device.
238 HRESULT hr = audio_device->Activate(__uuidof(IAudioClient),
239 CLSCTX_INPROC_SERVER,
240 NULL,
241 audio_client.ReceiveVoid());
242 LOG_IF(ERROR, FAILED(hr)) << "IMMDevice::Activate: " << std::hex << hr;
243 return audio_client;
244 }
245
246 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698