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.
5 #include "media/audio/win/audio_device_listener_win.h"
7 #include <Audioclient.h>
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/system_monitor/system_monitor.h"
12 #include "base/win/scoped_co_mem.h"
13 #include "base/win/windows_version.h"
14 #include "media/audio/win/core_audio_util_win.h"
16 using base::win::ScopedCoMem;
20 static std::string FlowToString(EDataFlow flow) {
21 return (flow == eRender) ? "eRender" : "eConsole";
24 static std::string RoleToString(ERole role) {
26 case eConsole: return "eConsole";
27 case eMultimedia: return "eMultimedia";
28 case eCommunications: return "eCommunications";
29 default: return "undefined";
33 AudioDeviceListenerWin::AudioDeviceListenerWin(const base::Closure& listener_cb)
34 : listener_cb_(listener_cb) {
35 CHECK(CoreAudioUtil::IsSupported());
37 ScopedComPtr<IMMDeviceEnumerator> device_enumerator(
38 CoreAudioUtil::CreateDeviceEnumerator());
39 if (!device_enumerator)
42 HRESULT hr = device_enumerator->RegisterEndpointNotificationCallback(this);
44 LOG(ERROR) << "RegisterEndpointNotificationCallback failed: "
49 device_enumerator_ = device_enumerator;
51 ScopedComPtr<IMMDevice> device =
52 CoreAudioUtil::CreateDefaultDevice(eRender, eConsole);
54 // Most probable reason for ending up here is that all audio devices are
55 // disabled or unplugged.
56 VLOG(1) << "CoreAudioUtil::CreateDefaultDevice failed. No device?";
60 AudioDeviceName device_name;
61 hr = CoreAudioUtil::GetDeviceName(device, &device_name);
63 VLOG(1) << "Failed to retrieve the device id: " << std::hex << hr;
66 default_render_device_id_ = device_name.unique_id;
69 AudioDeviceListenerWin::~AudioDeviceListenerWin() {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 if (device_enumerator_) {
73 device_enumerator_->UnregisterEndpointNotificationCallback(this);
74 LOG_IF(ERROR, FAILED(hr)) << "UnregisterEndpointNotificationCallback() "
75 << "failed: " << std::hex << hr;
79 STDMETHODIMP_(ULONG) AudioDeviceListenerWin::AddRef() {
83 STDMETHODIMP_(ULONG) AudioDeviceListenerWin::Release() {
87 STDMETHODIMP AudioDeviceListenerWin::QueryInterface(REFIID iid, void** object) {
88 if (iid == IID_IUnknown || iid == __uuidof(IMMNotificationClient)) {
89 *object = static_cast<IMMNotificationClient*>(this);
97 STDMETHODIMP AudioDeviceListenerWin::OnPropertyValueChanged(
98 LPCWSTR device_id, const PROPERTYKEY key) {
99 // TODO(dalecurtis): We need to handle changes for the current default device
100 // here. It's tricky because this method may be called many (20+) times for
101 // a single change like sample rate. http://crbug.com/153056
105 STDMETHODIMP AudioDeviceListenerWin::OnDeviceAdded(LPCWSTR device_id) {
106 // We don't care when devices are added.
110 STDMETHODIMP AudioDeviceListenerWin::OnDeviceRemoved(LPCWSTR device_id) {
111 // We don't care when devices are removed.
115 STDMETHODIMP AudioDeviceListenerWin::OnDeviceStateChanged(LPCWSTR device_id,
117 if (new_state != DEVICE_STATE_ACTIVE && new_state != DEVICE_STATE_NOTPRESENT)
120 base::SystemMonitor* monitor = base::SystemMonitor::Get();
122 monitor->ProcessDevicesChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
127 STDMETHODIMP AudioDeviceListenerWin::OnDefaultDeviceChanged(
128 EDataFlow flow, ERole role, LPCWSTR new_default_device_id) {
129 // Only listen for output device changes right now...
130 if (flow != eConsole && role != eRender)
133 // If no device is now available, |new_default_device_id| will be NULL.
134 std::string new_device_id;
135 if (new_default_device_id)
136 new_device_id = WideToUTF8(new_default_device_id);
138 VLOG(1) << "OnDefaultDeviceChanged() "
139 << "new_default_device: "
140 << (new_default_device_id ?
141 CoreAudioUtil::GetFriendlyName(new_device_id) : "No device")
142 << ", flow: " << FlowToString(flow)
143 << ", role: " << RoleToString(role);
145 // Only fire a state change event if the device has actually changed.
146 // TODO(dalecurtis): This still seems to fire an extra event on my machine for
147 // an unplug event (probably others too); e.g., we get two transitions to a
148 // new default device id.
149 if (new_device_id.compare(default_render_device_id_) == 0)
152 default_render_device_id_ = new_device_id;