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

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

Issue 187593004: Hook up AudioDeviceListenerWin to monitor all default device changes. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase Created 6 years, 9 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
« no previous file with comments | « media/audio/win/audio_device_listener_win.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
index 17f7730d876a2340b61668d0372c4c239fbc7840..ecf838748677b58bc8ecc95484144e6434522d00 100644
--- a/media/audio/win/audio_device_listener_win.cc
+++ b/media/audio/win/audio_device_listener_win.cc
@@ -30,6 +30,27 @@ static std::string RoleToString(ERole role) {
}
}
+static std::string GetDeviceId(EDataFlow flow,
+ ERole role) {
+ ScopedComPtr<IMMDevice> device =
+ CoreAudioUtil::CreateDefaultDevice(flow, role);
+ if (!device) {
+ // Most probable reason for ending up here is that all audio devices are
+ // disabled or unplugged.
+ DVLOG(1) << "CoreAudioUtil::CreateDefaultDevice failed. No device?";
+ return std::string();
+ }
+
+ AudioDeviceName device_name;
+ HRESULT hr = CoreAudioUtil::GetDeviceName(device, &device_name);
+ if (FAILED(hr)) {
+ DVLOG(1) << "Failed to retrieve the device id: " << std::hex << hr;
+ return std::string();
+ }
+
+ return device_name.unique_id;
+}
+
AudioDeviceListenerWin::AudioDeviceListenerWin(const base::Closure& listener_cb)
: listener_cb_(listener_cb) {
CHECK(CoreAudioUtil::IsSupported());
@@ -48,22 +69,12 @@ AudioDeviceListenerWin::AudioDeviceListenerWin(const base::Closure& listener_cb)
device_enumerator_ = device_enumerator;
- ScopedComPtr<IMMDevice> device =
- CoreAudioUtil::CreateDefaultDevice(eRender, eConsole);
- if (!device) {
- // Most probable reason for ending up here is that all audio devices are
- // disabled or unplugged.
- VLOG(1) << "CoreAudioUtil::CreateDefaultDevice failed. No device?";
- return;
- }
-
- AudioDeviceName device_name;
- hr = CoreAudioUtil::GetDeviceName(device, &device_name);
- if (FAILED(hr)) {
- VLOG(1) << "Failed to retrieve the device id: " << std::hex << hr;
- return;
- }
- default_render_device_id_ = device_name.unique_id;
+ default_render_device_id_ = GetDeviceId(eRender, eConsole);
+ default_capture_device_id_ = GetDeviceId(eCapture, eConsole);
+ default_communications_render_device_id_ =
+ GetDeviceId(eRender, eCommunications);
+ default_communications_capture_device_id_ =
+ GetDeviceId(eCapture, eCommunications);
}
AudioDeviceListenerWin::~AudioDeviceListenerWin() {
@@ -126,9 +137,24 @@ STDMETHODIMP AudioDeviceListenerWin::OnDeviceStateChanged(LPCWSTR device_id,
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)
+ // Only listen for console and communication device changes.
+ if ((role != eConsole && role != eCommunications) ||
+ (flow != eRender && flow != eCapture)) {
return S_OK;
+ }
+
+ // Grab a pointer to the appropriate ID member.
+ // Note that there are three "?:"'s here to select the right ID.
+ std::string* current_device_id =
+ role == eRender ? (
Nico 2014/07/26 04:43:52 this has to be eConsole,
+ flow == eConsole ?
Nico 2014/07/26 04:43:52 and this
+ &default_render_device_id_ :
+ &default_communications_render_device_id_
+ ) : (
+ flow == eConsole ?
Nico 2014/07/26 04:43:52 and this should be eRender, right? (both have the
+ &default_capture_device_id_ :
+ &default_communications_capture_device_id_
+ );
// If no device is now available, |new_default_device_id| will be NULL.
std::string new_device_id;
@@ -146,10 +172,11 @@ STDMETHODIMP AudioDeviceListenerWin::OnDefaultDeviceChanged(
// 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.
- if (new_device_id.compare(default_render_device_id_) == 0)
+ if (new_device_id.compare(*current_device_id) == 0)
return S_OK;
- default_render_device_id_ = new_device_id;
+ // Store the new id in the member variable (that current_device_id points to).
+ *current_device_id = new_device_id;
listener_cb_.Run();
return S_OK;
« no previous file with comments | « media/audio/win/audio_device_listener_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698