Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Unified Diff: media/audio/win/core_audio_util_win.cc

Issue 11226057: Adds Core Audio Utility methods for Windows (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed unit test based on feedback from Tommi Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/audio/win/core_audio_util_win.cc
diff --git a/media/audio/win/core_audio_util_win.cc b/media/audio/win/core_audio_util_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bfe0fe47dac31a4b0a3d93e36dd3289e39d375ca
--- /dev/null
+++ b/media/audio/win/core_audio_util_win.cc
@@ -0,0 +1,248 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/audio/win/core_audio_util_win.h"
+
+#include <Audioclient.h>
+#include <Functiondiscoverykeys_devpkey.h>
+
+#include "base/logging.h"
+#include "base/stringprintf.h"
+#include "base/utf_string_conversions.h"
+#include "base/win/scoped_co_mem.h"
+#include "base/win/windows_version.h"
+
+using base::win::ScopedCoMem;
+
+namespace media {
+
+#define PRINT(sz, ...) wprintf(sz L"\n", __VA_ARGS__);
scherkus (not reviewing) 2012/10/25 16:52:13 not removed :)
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
+
+class ScopedPropertyVariant {
+ public:
+ ScopedPropertyVariant() {
+ PropVariantInit(&prop_);
+ }
+ ~ScopedPropertyVariant() {
+ PropVariantClear(&prop_);
+ }
+
+ PROPVARIANT* Get() {
+ return &prop_;
+ }
+
+ private:
+ PROPVARIANT prop_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedPropertyVariant);
+};
+
+bool CoreAudioUtil::CoreAudioIsSupported() {
scherkus (not reviewing) 2012/10/25 16:52:13 TODO-ify de-duping?
henrika (OOO until Aug 14) 2012/10/26 17:51:56 Done.
+ // Microsoft does not plan to make the Core Audio APIs available for use
+ // with earlier versions of Windows, including Microsoft Windows Server 2003,
+ // Windows XP, Windows Millennium Edition, Windows 2000, and Windows 98.
+ return (base::win::GetVersion() >= base::win::VERSION_VISTA);
+}
+
+int CoreAudioUtil::NumberOfActiveDevices(EDataFlow data_flow) {
+ DCHECK(CoreAudioIsSupported());
+ // Create the IMMDeviceEnumerator interface.
+ ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
+ CreateDeviceEnumerator();
+ if (!device_enumerator)
+ return 0;
+
+ // Generate a collection of active (present and not disabled) audio endpoint
+ // devices for the specified data-flow direction.
+ // This method will succeed even if all devices are disabled.
+ ScopedComPtr<IMMDeviceCollection> collection;
+ HRESULT hr = device_enumerator->EnumAudioEndpoints(data_flow,
+ DEVICE_STATE_ACTIVE,
+ collection.Receive());
+ if (FAILED(hr)) {
+ LOG(ERROR) << "IMMDeviceCollection::EnumAudioEndpoints: " << std::hex << hr;
+ return 0;
+ }
+
+ // Retrieve the number of active audio devices for the specified direction
+ UINT number_of_active_devices = 0;
+ collection->GetCount(&number_of_active_devices);
+ std::string flow = (data_flow == eCapture) ? "[in ] " : "[out] ";
+ DVLOG(1) << flow << "number of devices: " << number_of_active_devices;
+ return static_cast<int>(number_of_active_devices);
+}
+
+ScopedComPtr<IMMDeviceEnumerator> CoreAudioUtil::CreateDeviceEnumerator() {
+ DCHECK(CoreAudioIsSupported());
+ ScopedComPtr<IMMDeviceEnumerator> device_enumerator;
+ HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
+ NULL,
+ CLSCTX_INPROC_SERVER,
+ __uuidof(IMMDeviceEnumerator),
+ device_enumerator.ReceiveVoid());
+ LOG_IF(ERROR, FAILED(hr)) << "CoCreateInstance(IMMDeviceEnumerator): "
+ << std::hex << hr;
+ return device_enumerator;
+}
+
+ScopedComPtr<IMMDevice> CoreAudioUtil::CreateDefaultDevice(EDataFlow data_flow,
+ ERole role) {
+ DCHECK(CoreAudioIsSupported());
+ ScopedComPtr<IMMDevice> endpoint_device;
+
+ // Create the IMMDeviceEnumerator interface.
+ ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
+ CreateDeviceEnumerator();
+ if (!device_enumerator)
+ return endpoint_device;
+
+ // Retrieve the default audio endpoint for the specified data-flow
+ // direction and role.
+ HRESULT hr = device_enumerator->GetDefaultAudioEndpoint(
+ data_flow, role, endpoint_device.Receive());
+
+ if (FAILED(hr)) {
+ LOG(ERROR) << "IMMDeviceEnumerator::GetDefaultAudioEndpoint: "
+ << std::hex << hr;
+ return endpoint_device;
+ }
+
+ // Verify that the audio endpoint device is active, i.e., that the audio
+ // adapter that connects to the endpoint device is present and enabled.
+ DWORD state = DEVICE_STATE_DISABLED;
+ hr = endpoint_device->GetState(&state);
+ if (SUCCEEDED(hr)) {
+ if (!(state & DEVICE_STATE_ACTIVE)) {
+ LOG(ERROR) << "Selected endpoint device is not active";
+ endpoint_device.Release();
+ }
+ }
+ return endpoint_device;
+}
+
+ScopedComPtr<IMMDevice> CoreAudioUtil::CreateDevice(
+ const std::string& device_id) {
+ DCHECK(CoreAudioIsSupported());
+ ScopedComPtr<IMMDevice> endpoint_device;
+
+ // Create the IMMDeviceEnumerator interface.
+ ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
+ CreateDeviceEnumerator();
+ if (!device_enumerator)
+ return endpoint_device;
+
+ // Retrieve an audio device specified by an endpoint device-identification
+ // string.
+ HRESULT hr = device_enumerator->GetDevice(UTF8ToUTF16(device_id).c_str(),
+ endpoint_device.Receive());
+ LOG_IF(ERROR, FAILED(hr)) << "IMMDeviceEnumerator::GetDevice: "
+ << std::hex << hr;
+ return endpoint_device;
+}
+
+HRESULT CoreAudioUtil::GetDeviceName(IMMDevice* device, AudioDeviceName* name) {
+ DCHECK(CoreAudioIsSupported());
+ DCHECK(device);
+ AudioDeviceName device_name;
+
+ // Retrieve unique name of endpoint device.
+ // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}".
+ ScopedCoMem<WCHAR> endpoint_device_id;
+ HRESULT hr = device->GetId(&endpoint_device_id);
+ if (FAILED(hr))
+ return hr;
+ device_name.unique_id = WideToUTF8(static_cast<WCHAR*>(endpoint_device_id));
+
+ // Retrieve user-friendly name of endpoint device.
+ // Example: "Microphone (Realtek High Definition Audio)".
+ ScopedComPtr<IPropertyStore> properties;
+ hr = device->OpenPropertyStore(STGM_READ, properties.Receive());
+ if (FAILED(hr))
+ return hr;
+ PROPVARIANT friendly_name;
+ PropVariantInit(&friendly_name);
+ hr = properties->GetValue(PKEY_Device_FriendlyName, &friendly_name);
+ if (FAILED(hr))
+ return hr;
+ if (friendly_name.vt == VT_LPWSTR && friendly_name.pwszVal) {
+ device_name.device_name = WideToUTF8(friendly_name.pwszVal);
+ }
+ PropVariantClear(&friendly_name);
+
+ *name = device_name;
+ DVLOG(1) << "friendly name: " << device_name.device_name;
+ DVLOG(1) << "unique id : " << device_name.unique_id;
+ return hr;
+}
+
+std::string CoreAudioUtil::GetFriendlyName(const std::string& device_id) {
+ DCHECK(CoreAudioIsSupported());
+ std::string name;
+
+ ScopedComPtr<IMMDevice> audio_device = CreateDevice(device_id);
+ if (!audio_device)
+ return name;
+
+ AudioDeviceName device_name;
+ HRESULT hr = GetDeviceName(audio_device, &device_name);
+ if (FAILED(hr))
+ return name;
+
+ name = device_name.device_name;
+ return name;
+}
+
+bool CoreAudioUtil::DeviceIsDefault(EDataFlow flow,
+ ERole role,
+ std::string device_id) {
+ DCHECK(CoreAudioIsSupported());
+ ScopedComPtr<IMMDevice> device = CreateDefaultDevice(flow, role);
+ if (!device)
+ return false;
+
+ ScopedCoMem<WCHAR> default_device_id;
+ HRESULT hr = device->GetId(&default_device_id);
+ if (FAILED(hr))
+ return false;
+
+ std::string str_default = WideToUTF8(static_cast<WCHAR*>(default_device_id));
+ if (device_id.compare(str_default) != 0) {
+ return false;
+ }
+ return true;
+}
+
+EDataFlow CoreAudioUtil::GetDataFlow(IMMDevice* device) {
+ DCHECK(CoreAudioIsSupported());
+ if (!device)
+ return eAll;
+
+ ScopedComPtr<IMMEndpoint> endpoint;
+ device->QueryInterface(endpoint.Receive());
+
+ EDataFlow data_flow;
+ HRESULT hr = endpoint->GetDataFlow(&data_flow);
+ if (FAILED(hr)) {
+ LOG(ERROR) << "IMMEndpoint::GetDataFlow: " << std::hex << hr;
+ return eAll;
+ }
+ return data_flow;
+}
+
+ScopedComPtr<IAudioClient> CoreAudioUtil::CreateClient(
+ IMMDevice* audio_device) {
+ DCHECK(CoreAudioIsSupported());
+ ScopedComPtr<IAudioClient> audio_client;
+
+ // Creates and activates an IAudioClient COM object given the selected
+ // endpoint device.
+ HRESULT hr = audio_device->Activate(__uuidof(IAudioClient),
+ CLSCTX_INPROC_SERVER,
+ NULL,
+ audio_client.ReceiveVoid());
+ LOG_IF(ERROR, FAILED(hr)) << "IMMDevice::Activate: " << std::hex << hr;
+ return audio_client;
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698