Chromium Code Reviews| Index: media/audio/win/audio_device_listener_win.cc |
| diff --git a/media/audio/win/audio_device_listener_win.cc b/media/audio/win/audio_device_listener_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d3e9933edeed9310cc67f43d9fe841d96aec8b39 |
| --- /dev/null |
| +++ b/media/audio/win/audio_device_listener_win.cc |
| @@ -0,0 +1,143 @@ |
| +// 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/audio_device_listener_win.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/win/scoped_co_mem.h" |
| +#include "base/win/windows_version.h" |
| + |
| +#include <Audioclient.h> |
| + |
| +using base::win::ScopedCoMem; |
| + |
| +namespace media { |
| + |
| +// TODO(henrika): Move to CoreAudioUtil class. |
| +static ScopedComPtr<IMMDeviceEnumerator> CreateDeviceEnumerator() { |
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator; |
| + HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), |
| + NULL, |
| + CLSCTX_INPROC_SERVER, |
| + __uuidof(IMMDeviceEnumerator), |
| + device_enumerator.ReceiveVoid()); |
| + DLOG_IF(ERROR, FAILED(hr)) << "CoCreateInstance(IMMDeviceEnumerator): " |
| + << std::hex << hr; |
| + return device_enumerator; |
| +} |
| + |
| +AudioDeviceListenerWin::AudioDeviceListenerWin( |
| + AudioManager::AudioDeviceListener* listener) |
| + : listener_(listener) { |
| + // Windows Core Audio is only supported on Vista or higher. |
| + if (base::win::GetVersion() < base::win::VERSION_VISTA) |
|
scherkus (not reviewing)
2012/10/24 04:27:09
FYI this is equivalent to calling !IsWASAPISupport
DaleCurtis
2012/10/24 19:27:29
Hah! Really?! I thought we were actually querying
henrika (OOO until Aug 14)
2012/10/24 19:49:22
I have not been able to come up with a better meth
|
| + return; |
| + |
| + device_enumerator_ = CreateDeviceEnumerator(); |
| + if (!device_enumerator_) |
| + return; |
| + |
| + HRESULT hr = device_enumerator_->RegisterEndpointNotificationCallback(this); |
| + if (FAILED(hr)) { |
| + DLOG(ERROR) << "RegisterEndpointNotificationCallback failed: " |
| + << std::hex << hr; |
| + return; |
| + } |
| + |
| + ScopedComPtr<IMMDevice> endpoint_render_device; |
| + hr = device_enumerator_->GetDefaultAudioEndpoint( |
| + eRender, eConsole, endpoint_render_device.Receive()); |
| + // This will fail if there are no audio devices currently plugged in, so we |
| + // still want to keep our endpoint registered. |
| + if (FAILED(hr)) { |
| + DLOG(ERROR) << "GetDefaultAudioEndpoint() failed. No devices? Error: " |
| + << std::hex << hr; |
| + return; |
| + } |
| + |
| + ScopedCoMem<WCHAR> render_device_id; |
| + hr = endpoint_render_device->GetId(&render_device_id); |
| + if (FAILED(hr)) { |
| + DLOG(ERROR) << "GetId() failed: " << std::hex << hr; |
| + return; |
| + } |
| + |
| + default_render_device_id_ = WideToUTF8(static_cast<WCHAR*>(render_device_id)); |
| + DVLOG(1) << "Default render device: " << default_render_device_id_; |
| +} |
| + |
| +AudioDeviceListenerWin::~AudioDeviceListenerWin() { |
| + if (device_enumerator_) |
| + device_enumerator_->UnregisterEndpointNotificationCallback(this); |
| +} |
| + |
| +STDMETHODIMP_(ULONG) AudioDeviceListenerWin::AddRef() { |
| + return 1; |
| +} |
| + |
| +STDMETHODIMP_(ULONG) AudioDeviceListenerWin::Release() { |
| + return 1; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::QueryInterface(REFIID iid, void** object) { |
| + if (iid == IID_IUnknown || iid == __uuidof(IMMNotificationClient)) { |
|
scherkus (not reviewing)
2012/10/24 04:27:09
I ain't a COM pro... but does this say if it's IUn
DaleCurtis
2012/10/24 19:27:29
This looks correct (minus a fix I just added) from
henrika (OOO until Aug 14)
2012/10/24 19:49:22
Working with COM usually result in that you must t
|
| + *object = static_cast < IMMNotificationClient*>(this); |
|
scherkus (not reviewing)
2012/10/24 04:27:09
remove extra spaces
DaleCurtis
2012/10/24 19:27:29
Done.
|
| + } else { |
| + return E_NOINTERFACE; |
| + } |
| + return S_OK; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::OnPropertyValueChanged( |
| + LPCWSTR device_id, const PROPERTYKEY key) { |
| + // TODO(dalecurtis): We need to handle changes for the current default device |
| + // here. It's tricky because this method may be called many (20+) times for |
| + // a single change like sample rate. http://crbug.com/153056 |
| + return S_OK; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::OnDeviceAdded(LPCWSTR device_id) { |
| + // We don't care when devices are added. |
| + return S_OK; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::OnDeviceRemoved(LPCWSTR device_id) { |
| + // We don't care when devices are removed. |
| + return S_OK; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::OnDeviceStateChanged(LPCWSTR device_id, |
| + DWORD new_state) { |
| + return S_OK; |
| +} |
| + |
| +STDMETHODIMP AudioDeviceListenerWin::OnDefaultDeviceChanged( |
| + EDataFlow flow, ERole role, LPCWSTR new_default_device_id) { |
| + // Only listen for output device changes right now... |
| + if (flow != eConsole && role != eRender) |
| + return S_OK; |
| + |
| + // If no device is now available, |new_default_device_id| will be NULL. |
| + std::string new_device_id = ""; |
| + if (new_default_device_id) { |
| + new_device_id = WideToUTF8(static_cast<const WCHAR*>( |
| + new_default_device_id)); |
| + } |
| + |
| + // Only fire a state change event if the device has actually changed. |
| + if (new_device_id.compare(default_render_device_id_) == 0) |
| + return S_OK; |
| + |
| + default_render_device_id_ = new_device_id; |
| + |
| + // TODO(dalecurtis): This still seems to fire an extra event on my machine for |
| + // an unplug event (probably others too); e.g., we get two transitions to a |
| + // new default device id. |
| + listener_->OnDeviceChange(); |
| + |
| + return S_OK; |
| +} |
| + |
| +} // namespace media |