OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/logging.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/win/scoped_co_mem.h" |
| 14 #include "base/win/scoped_comptr.h" |
| 15 |
| 16 using media::AudioDeviceNames; |
| 17 using base::win::ScopedComPtr; |
| 18 using base::win::ScopedCoMem; |
| 19 |
| 20 bool GetInputDeviceNamesWin(AudioDeviceNames* device_names) { |
| 21 // It is assumed that this method is called from a COM thread, i.e., |
| 22 // CoInitializeEx() is not called here again to avoid STA/MTA conflicts. |
| 23 ScopedComPtr<IMMDeviceEnumerator> enumerator; |
| 24 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), |
| 25 NULL, |
| 26 CLSCTX_INPROC_SERVER, |
| 27 __uuidof(IMMDeviceEnumerator), |
| 28 enumerator.ReceiveVoid()); |
| 29 if (FAILED(hr)) { |
| 30 LOG(WARNING) << "Failed to create IMMDeviceEnumerator: " << std::hex << hr; |
| 31 return false; |
| 32 } |
| 33 |
| 34 // Generate a collection of active audio capture endpoint devices. |
| 35 // This method will succeed even if all devices are disabled. |
| 36 ScopedComPtr<IMMDeviceCollection> collection; |
| 37 hr = enumerator->EnumAudioEndpoints(eCapture, |
| 38 DEVICE_STATE_ACTIVE, |
| 39 collection.Receive()); |
| 40 if (FAILED(hr)) |
| 41 return false; |
| 42 |
| 43 // Retrieve the number of active capture devices. |
| 44 UINT number_of_active_devices = 0; |
| 45 collection->GetCount(&number_of_active_devices); |
| 46 if (number_of_active_devices == 0) |
| 47 return true; |
| 48 |
| 49 media::AudioDeviceName device; |
| 50 |
| 51 // Loop over all active capture devices and add friendly name and |
| 52 // unique ID to the |device_names| list. |
| 53 for (UINT i = 0; i < number_of_active_devices; ++i) { |
| 54 // Retrieve unique name of endpoint device. |
| 55 // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}". |
| 56 ScopedComPtr<IMMDevice> audio_device; |
| 57 ScopedCoMem<WCHAR> endpoint_device_id; |
| 58 hr = collection->Item(i, audio_device.Receive()); |
| 59 if (FAILED(hr)) |
| 60 continue; |
| 61 audio_device->GetId(&endpoint_device_id); |
| 62 |
| 63 // Store the unique name. |
| 64 device.unique_id = WideToUTF8(static_cast<WCHAR*>(endpoint_device_id)); |
| 65 |
| 66 // Retrieve user-friendly name of endpoint device. |
| 67 // Example: "Microphone (Realtek High Definition Audio)". |
| 68 ScopedComPtr<IPropertyStore> properties; |
| 69 hr = audio_device->OpenPropertyStore(STGM_READ, properties.Receive()); |
| 70 if (SUCCEEDED(hr)) { |
| 71 PROPVARIANT friendly_name; |
| 72 PropVariantInit(&friendly_name); |
| 73 hr = properties->GetValue(PKEY_Device_FriendlyName, &friendly_name); |
| 74 |
| 75 // Store the user-friendly name. |
| 76 if (SUCCEEDED(hr) && |
| 77 friendly_name.vt == VT_LPWSTR && friendly_name.pwszVal) { |
| 78 device.device_name = WideToUTF8(friendly_name.pwszVal); |
| 79 } |
| 80 PropVariantClear(&friendly_name); |
| 81 } |
| 82 |
| 83 // Add combination of user-friendly and unique name to the output list. |
| 84 device_names->push_back(device); |
| 85 } |
| 86 |
| 87 return true; |
| 88 } |
| 89 |
| 90 bool GetInputDeviceNamesWinXP(AudioDeviceNames* device_names) { |
| 91 // Retrieve the number of active waveform input devices. |
| 92 UINT number_of_active_devices = waveInGetNumDevs(); |
| 93 if (number_of_active_devices == 0) |
| 94 return true; |
| 95 |
| 96 media::AudioDeviceName device; |
| 97 WAVEINCAPS capabilities; |
| 98 MMRESULT err = MMSYSERR_NOERROR; |
| 99 |
| 100 // Loop over all active capture devices and add friendly name and |
| 101 // unique ID to the |device_names| list. Note that, for Wave on XP, |
| 102 // the "unique" name will simply be a copy of the friendly name since |
| 103 // there is no safe method to retrieve a unique device name on XP. |
| 104 for (UINT i = 0; i < number_of_active_devices; ++i) { |
| 105 // Retrieve the capabilities of the specified waveform-audio input device. |
| 106 err = waveInGetDevCaps(i, &capabilities, sizeof(capabilities)); |
| 107 if (err != MMSYSERR_NOERROR) |
| 108 continue; |
| 109 |
| 110 if (capabilities.szPname != NULL) { |
| 111 // Store the user-friendly name. Max length is MAXPNAMELEN(=32) |
| 112 // characters and the name cane be truncated on XP. |
| 113 // Example: "Microphone (Realtek High Defini". |
| 114 device.device_name = WideToUTF8(capabilities.szPname); |
| 115 |
| 116 // Store the "unique" name (we use same as friendly name on Windows XP). |
| 117 device.unique_id = WideToUTF8(capabilities.szPname); |
| 118 |
| 119 // Add combination of user-friendly and unique name to the output list. |
| 120 device_names->push_back(device); |
| 121 } |
| 122 } |
| 123 |
| 124 return true; |
| 125 } |
OLD | NEW |