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