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 <MMDeviceAPI.h> 6 #include <mmsystem.h> 7 #include <Functiondiscoverykeys_devpkey.h> // MMDeviceAPI.h must come first 8 9 #include "media/audio/win/audio_manager_win.h" 10 11 #include "base/basictypes.h" 12 #include "base/logging.h" 13 #include "base/strings/utf_string_conversions.h" 14 #include "base/win/scoped_co_mem.h" 15 #include "base/win/scoped_comptr.h" 16 #include "base/win/scoped_propvariant.h" 17 18 using base::win::ScopedComPtr; 19 using base::win::ScopedCoMem; 20 21 // Taken from Mmddk.h. 22 #define DRV_RESERVED 0x0800 23 #define DRV_QUERYFUNCTIONINSTANCEID (DRV_RESERVED + 17) 24 #define DRV_QUERYFUNCTIONINSTANCEIDSIZE (DRV_RESERVED + 18) 25 26 namespace media { 27 28 static bool GetDeviceNamesWinImpl(EDataFlow data_flow, 29 AudioDeviceNames* device_names) { 30 // It is assumed that this method is called from a COM thread, i.e., 31 // CoInitializeEx() is not called here again to avoid STA/MTA conflicts. 32 ScopedComPtr<IMMDeviceEnumerator> enumerator; 33 HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator), NULL, 34 CLSCTX_INPROC_SERVER); 35 DCHECK_NE(CO_E_NOTINITIALIZED, hr); 36 if (FAILED(hr)) { 37 LOG(WARNING) << "Failed to create IMMDeviceEnumerator: " << std::hex << hr; 38 return false; 39 } 40 41 // Generate a collection of active audio endpoint devices. 42 // This method will succeed even if all devices are disabled. 43 ScopedComPtr<IMMDeviceCollection> collection; 44 hr = enumerator->EnumAudioEndpoints(data_flow, 45 DEVICE_STATE_ACTIVE, 46 collection.Receive()); 47 if (FAILED(hr)) 48 return false; 49 50 // Retrieve the number of active devices. 51 UINT number_of_active_devices = 0; 52 collection->GetCount(&number_of_active_devices); 53 if (number_of_active_devices == 0) 54 return true; 55 56 AudioDeviceName device; 57 58 // Loop over all active devices and add friendly name and 59 // unique ID to the |device_names| list. 60 for (UINT i = 0; i < number_of_active_devices; ++i) { 61 // Retrieve unique name of endpoint device. 62 // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}". 63 ScopedComPtr<IMMDevice> audio_device; 64 hr = collection->Item(i, audio_device.Receive()); 65 if (FAILED(hr)) 66 continue; 67 68 // Store the unique name. 69 ScopedCoMem<WCHAR> endpoint_device_id; 70 audio_device->GetId(&endpoint_device_id); 71 device.unique_id = WideToUTF8(static_cast<WCHAR*>(endpoint_device_id)); 72 73 // Retrieve user-friendly name of endpoint device. 74 // Example: "Microphone (Realtek High Definition Audio)". 75 ScopedComPtr<IPropertyStore> properties; 76 hr = audio_device->OpenPropertyStore(STGM_READ, properties.Receive()); 77 if (SUCCEEDED(hr)) { 78 base::win::ScopedPropVariant friendly_name; 79 hr = properties->GetValue(PKEY_Device_FriendlyName, 80 friendly_name.Receive()); 81 82 // Store the user-friendly name. 83 if (SUCCEEDED(hr) && 84 friendly_name.get().vt == VT_LPWSTR && friendly_name.get().pwszVal) { 85 device.device_name = WideToUTF8(friendly_name.get().pwszVal); 86 } 87 } 88 89 // Add combination of user-friendly and unique name to the output list. 90 device_names->push_back(device); 91 } 92 93 return true; 94 } 95 96 // The waveform API is weird in that it has completely separate but 97 // almost identical functions and structs for input devices vs. output 98 // devices. We deal with this by implementing the logic as a templated 99 // function that takes the functions and struct type to use as 100 // template parameters. 101 template <UINT (__stdcall *NumDevsFunc)(), 102 typename CAPSSTRUCT, 103 MMRESULT (__stdcall *DevCapsFunc)(UINT_PTR, CAPSSTRUCT*, UINT)> 104 static bool GetDeviceNamesWinXPImpl(AudioDeviceNames* device_names) { 105 // Retrieve the number of active waveform input devices. 106 UINT number_of_active_devices = NumDevsFunc(); 107 if (number_of_active_devices == 0) 108 return true; 109 110 AudioDeviceName device; 111 CAPSSTRUCT capabilities; 112 MMRESULT err = MMSYSERR_NOERROR; 113 114 // Loop over all active capture devices and add friendly name and 115 // unique ID to the |device_names| list. Note that, for Wave on XP, 116 // the "unique" name will simply be a copy of the friendly name since 117 // there is no safe method to retrieve a unique device name on XP. 118 for (UINT i = 0; i < number_of_active_devices; ++i) { 119 // Retrieve the capabilities of the specified waveform-audio input device. 120 err = DevCapsFunc(i, &capabilities, sizeof(capabilities)); 121 if (err != MMSYSERR_NOERROR) 122 continue; 123 124 // Store the user-friendly name. Max length is MAXPNAMELEN(=32) 125 // characters and the name cane be truncated on XP. 126 // Example: "Microphone (Realtek High Defini". 127 device.device_name = WideToUTF8(capabilities.szPname); 128 129 // Store the "unique" name (we use same as friendly name on Windows XP). 130 device.unique_id = device.device_name; 131 132 // Add combination of user-friendly and unique name to the output list. 133 device_names->push_back(device); 134 } 135 136 return true; 137 } 138 139 bool GetInputDeviceNamesWin(AudioDeviceNames* device_names) { 140 return GetDeviceNamesWinImpl(eCapture, device_names); 141 } 142 143 bool GetOutputDeviceNamesWin(AudioDeviceNames* device_names) { 144 return GetDeviceNamesWinImpl(eRender, device_names); 145 } 146 147 bool GetInputDeviceNamesWinXP(AudioDeviceNames* device_names) { 148 return GetDeviceNamesWinXPImpl< 149 waveInGetNumDevs, WAVEINCAPSW, waveInGetDevCapsW>(device_names); 150 } 151 152 bool GetOutputDeviceNamesWinXP(AudioDeviceNames* device_names) { 153 return GetDeviceNamesWinXPImpl< 154 waveOutGetNumDevs, WAVEOUTCAPSW, waveOutGetDevCapsW>(device_names); 155 } 156 157 std::string ConvertToWinXPInputDeviceId(const std::string& device_id) { 158 UINT number_of_active_devices = waveInGetNumDevs(); 159 MMRESULT result = MMSYSERR_NOERROR; 160 161 UINT i = 0; 162 for (; i < number_of_active_devices; ++i) { 163 size_t size = 0; 164 // Get the size (including the terminating NULL) of the endpoint ID of the 165 // waveIn device. 166 result = waveInMessage(reinterpret_cast<HWAVEIN>(i), 167 DRV_QUERYFUNCTIONINSTANCEIDSIZE, 168 reinterpret_cast<DWORD_PTR>(&size), NULL); 169 if (result != MMSYSERR_NOERROR) 170 continue; 171 172 ScopedCoMem<WCHAR> id; 173 id.Reset(static_cast<WCHAR*>(CoTaskMemAlloc(size))); 174 if (!id) 175 continue; 176 177 // Get the endpoint ID string for this waveIn device. 178 result = waveInMessage( 179 reinterpret_cast<HWAVEIN>(i), DRV_QUERYFUNCTIONINSTANCEID, 180 reinterpret_cast<DWORD_PTR>(static_cast<WCHAR*>(id)), size); 181 if (result != MMSYSERR_NOERROR) 182 continue; 183 184 std::string utf8_id = WideToUTF8(static_cast<WCHAR*>(id)); 185 // Check whether the endpoint ID string of this waveIn device matches that 186 // of the audio endpoint device. 187 if (device_id == utf8_id) 188 break; 189 } 190 191 // If a matching waveIn device was found, convert the unique endpoint ID 192 // string to a standard friendly name with max 32 characters. 193 if (i < number_of_active_devices) { 194 WAVEINCAPS capabilities; 195 196 result = waveInGetDevCaps(i, &capabilities, sizeof(capabilities)); 197 if (result == MMSYSERR_NOERROR) 198 return WideToUTF8(capabilities.szPname); 199 } 200 201 return std::string(); 202 } 203 204 } // namespace media 205