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()) { |
| 182 WideToUTF8(friendly_name.as_wide_string(), |
| 183 wcslen(friendly_name.as_wide_string()), |
| 184 &device_name.device_name); |
| 185 } |
| 186 |
| 187 *name = device_name; |
| 188 DVLOG(1) << "friendly name: " << device_name.device_name; |
| 189 DVLOG(1) << "unique id : " << device_name.unique_id; |
| 190 return hr; |
| 191 } |
| 192 |
| 193 std::string CoreAudioUtil::GetFriendlyName(const std::string& device_id) { |
| 194 DCHECK(CoreAudioUtil::IsSupported()); |
| 195 |
| 196 ScopedComPtr<IMMDevice> audio_device = CreateDevice(device_id); |
| 197 if (!audio_device) |
| 198 return std::string(); |
| 199 |
| 200 AudioDeviceName device_name; |
| 201 HRESULT hr = GetDeviceName(audio_device, &device_name); |
| 202 if (FAILED(hr)) |
| 203 return std::string(); |
| 204 |
| 205 return device_name.device_name; |
| 206 } |
| 207 |
| 208 bool CoreAudioUtil::DeviceIsDefault(EDataFlow flow, |
| 209 ERole role, |
| 210 std::string device_id) { |
| 211 DCHECK(CoreAudioUtil::IsSupported()); |
| 212 ScopedComPtr<IMMDevice> device = CreateDefaultDevice(flow, role); |
| 213 if (!device) |
| 214 return false; |
| 215 |
| 216 ScopedCoMem<WCHAR> default_device_id; |
| 217 HRESULT hr = device->GetId(&default_device_id); |
| 218 if (FAILED(hr)) |
| 219 return false; |
| 220 |
| 221 std::string str_default; |
| 222 WideToUTF8(default_device_id, wcslen(default_device_id), &str_default); |
| 223 if (device_id.compare(str_default) != 0) |
| 224 return false; |
| 225 return true; |
| 226 } |
| 227 |
| 228 EDataFlow CoreAudioUtil::GetDataFlow(IMMDevice* device) { |
| 229 DCHECK(CoreAudioUtil::IsSupported()); |
| 230 DCHECK(device); |
| 231 |
| 232 ScopedComPtr<IMMEndpoint> endpoint; |
| 233 HRESULT hr = device->QueryInterface(endpoint.Receive()); |
| 234 if (FAILED(hr)) { |
| 235 DVLOG(1) << "IMMDevice::QueryInterface: " << std::hex << hr; |
| 236 return eAll; |
| 237 } |
| 238 |
| 239 EDataFlow data_flow; |
| 240 hr = endpoint->GetDataFlow(&data_flow); |
| 241 if (FAILED(hr)) { |
| 242 DVLOG(1) << "IMMEndpoint::GetDataFlow: " << std::hex << hr; |
| 243 return eAll; |
| 244 } |
| 245 return data_flow; |
| 246 } |
| 247 |
| 248 ScopedComPtr<IAudioClient> CoreAudioUtil::CreateClient( |
| 249 IMMDevice* audio_device) { |
| 250 DCHECK(CoreAudioUtil::IsSupported()); |
| 251 ScopedComPtr<IAudioClient> audio_client; |
| 252 |
| 253 // Creates and activates an IAudioClient COM object given the selected |
| 254 // endpoint device. |
| 255 HRESULT hr = audio_device->Activate(__uuidof(IAudioClient), |
| 256 CLSCTX_INPROC_SERVER, |
| 257 NULL, |
| 258 audio_client.ReceiveVoid()); |
| 259 DVLOG_IF(1, FAILED(hr)) << "IMMDevice::Activate: " << std::hex << hr; |
| 260 return audio_client; |
| 261 } |
| 262 |
| 263 } // namespace media |
OLD | NEW |