1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #ifndef MUMBLE_MUMBLE_WASAPINOTIFICATIONCLIENT_H_
7 #define MUMBLE_MUMBLE_WASAPINOTIFICATIONCLIENT_H_
8 
9 #include <QtCore/QObject>
10 #include <QtCore/QMutex>
11 #include <mmdeviceapi.h>
12 
13 /**
14  * @brief Singleton for acting on WASAPINotification events for given devices.
15  */
16 class WASAPINotificationClient : public QObject, public IMMNotificationClient {
17 	Q_OBJECT
18 public:
19 	/* IMMNotificationClient interface */
20 	HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDevice);
21 	HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key);
22 	HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId);
23 	HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId);
24 	HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState);
25 	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface);
26 	ULONG STDMETHODCALLTYPE AddRef();
27 	ULONG STDMETHODCALLTYPE Release();
28 
29 	/* Enlist/Unlist functionality */
30 	void enlistDefaultDeviceAsUsed(LPCWSTR pwstrDefaultDevice);
31 
32 	void enlistDeviceAsUsed(LPCWSTR pwstrDevice);
33 	void enlistDeviceAsUsed(const QString& device);
34 
35 	void unlistDevice(LPCWSTR pwstrDevice);
36 
37 	void clearUsedDefaultDeviceList();
38 	void clearUsedDeviceLists();
39 
40 	/**
41 	 * @return Singleton instance reference.
42 	 */
43 	static WASAPINotificationClient& get();
44 
45 private:
46 	WASAPINotificationClient();
47 	~WASAPINotificationClient() Q_DECL_OVERRIDE;
48 
49 	WASAPINotificationClient(const WASAPINotificationClient&);
50 	WASAPINotificationClient& operator=(const WASAPINotificationClient&);
51 
52 	static WASAPINotificationClient& doGet();
53 	static void doGetOnce();
54 
55 	void restartAudio();
56 
57 	/* _fu = Non locking versions */
58 	void _clearUsedDeviceLists();
59 	void _enlistDeviceAsUsed(const QString& device);
60 
61 	QStringList usedDefaultDevices;
62 	QStringList usedDevices;
63 	IMMDeviceEnumerator *pEnumerator;
64 	LONG _cRef;
65 	QMutex listsMutex;
66 
67 signals:
68 	void doResetAudio();
69 };
70 
71 #endif // WASAPINOTIFICATIONCLIENT_H_
72