audio_device_listener_win.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#ifndef MEDIA_AUDIO_WIN_AUDIO_DEVICE_LISTENER_WIN_H_
6#define MEDIA_AUDIO_WIN_AUDIO_DEVICE_LISTENER_WIN_H_
7
8#include <MMDeviceAPI.h>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/threading/thread_checker.h"
14#include "base/win/scoped_comptr.h"
15#include "media/base/media_export.h"
16
17using base::win::ScopedComPtr;
18
19namespace media {
20
21// IMMNotificationClient implementation for listening for default device changes
22// and forwarding to AudioManagerWin so it can notify downstream clients.  Only
23// output (eRender) device changes are supported currently.  Core Audio support
24// is required to construct this object.  Must be constructed and destructed on
25// a single COM initialized thread.
26// TODO(dalecurtis, henrika): Support input device changes.
27class MEDIA_EXPORT AudioDeviceListenerWin : public IMMNotificationClient {
28 public:
29  // The listener callback will be called from a system level multimedia thread,
30  // thus the callee must be thread safe.  |listener| is a permanent callback
31  // and must outlive AudioDeviceListenerWin.
32  explicit AudioDeviceListenerWin(const base::Closure& listener_cb);
33  virtual ~AudioDeviceListenerWin();
34
35 private:
36  friend class AudioDeviceListenerWinTest;
37
38  // IMMNotificationClient implementation.
39  STDMETHOD_(ULONG, AddRef)();
40  STDMETHOD_(ULONG, Release)();
41  STDMETHOD(QueryInterface)(REFIID iid, void** object);
42  STDMETHOD(OnPropertyValueChanged)(LPCWSTR device_id, const PROPERTYKEY key);
43  STDMETHOD(OnDeviceAdded)(LPCWSTR device_id);
44  STDMETHOD(OnDeviceRemoved)(LPCWSTR device_id);
45  STDMETHOD(OnDeviceStateChanged)(LPCWSTR device_id, DWORD new_state);
46  STDMETHOD(OnDefaultDeviceChanged)(EDataFlow flow, ERole role,
47                                    LPCWSTR new_default_device_id);
48
49  base::Closure listener_cb_;
50  ScopedComPtr<IMMDeviceEnumerator> device_enumerator_;
51  std::string default_render_device_id_;
52  std::string default_capture_device_id_;
53  std::string default_communications_render_device_id_;
54  std::string default_communications_capture_device_id_;
55
56  // AudioDeviceListenerWin must be constructed and destructed on one thread.
57  base::ThreadChecker thread_checker_;
58
59  DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerWin);
60};
61
62}  // namespace media
63
64#endif  // MEDIA_AUDIO_WIN_AUDIO_DEVICE_LISTENER_WIN_H_
65