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