Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WASAPINotificationClient.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34212865567f80d1e27ccea912c310cf476f3e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2012-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "MainWindow.h"

#include "WASAPINotificationClient.h"
#include "Global.h"

#include <QtCore/QMutexLocker>
#include <boost/thread/once.hpp>

HRESULT STDMETHODCALLTYPE WASAPINotificationClient::OnDefaultDeviceChanged(EDataFlow flow, ERole role,
																		   LPCWSTR pwstrDefaultDevice) {
	const QString device = QString::fromWCharArray(pwstrDefaultDevice);

	qWarning() << "WASAPINotificationClient: Default device changed flow=" << flow << "role=" << role << "device"
			   << device;

	QMutexLocker lock(&listsMutex);
	if (!usedDefaultDevices.empty() && role == eCommunications) {
		restartAudio();
	}
	return S_OK;
}

HRESULT STDMETHODCALLTYPE WASAPINotificationClient::OnPropertyValueChanged(LPCWSTR pwstrDeviceId,
																		   const PROPERTYKEY key) {
	const QString device = QString::fromWCharArray(pwstrDeviceId);

	const bool formatChanged        = (key == PKEY_AudioEngine_DeviceFormat);
	const bool channelConfigChanged = (key == PKEY_AudioEndpoint_PhysicalSpeakers);

	QMutexLocker lock(&listsMutex);
	if ((formatChanged || channelConfigChanged) && usedDevices.contains(device)) {
		qWarning() << "WASAPINotificationClient: Property changed device=" << device
				   << "formatChanged=" << formatChanged << "channelConfigChanged=" << channelConfigChanged;

		restartAudio();
	}
	return S_OK;
}

HRESULT STDMETHODCALLTYPE WASAPINotificationClient::OnDeviceAdded(LPCWSTR pwstrDeviceId) {
	const QString device = QString::fromWCharArray(pwstrDeviceId);
	qWarning() << "WASAPINotificationClient: Device added=" << device;
	return S_OK;
}
HRESULT STDMETHODCALLTYPE WASAPINotificationClient::OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
	const QString device = QString::fromWCharArray(pwstrDeviceId);
	qWarning() << "WASAPINotificationClient: Device removed=" << device;
	return S_OK;
}

HRESULT STDMETHODCALLTYPE WASAPINotificationClient::OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
	const QString device = QString::fromWCharArray(pwstrDeviceId);

	qWarning() << "WASAPINotificationClient: Device state changed newState=" << dwNewState << "device=" << device;

	return S_OK;
}

HRESULT STDMETHODCALLTYPE WASAPINotificationClient::QueryInterface(REFIID riid, VOID **ppvInterface) {
	if (IID_IUnknown == riid) {
		*ppvInterface = (IUnknown *) this;
		AddRef();
	} else if (__uuidof(IMMNotificationClient) == riid) {
		*ppvInterface = (IMMNotificationClient *) this;
		AddRef();
	} else {
		*ppvInterface = nullptr;
		return E_NOINTERFACE;
	}
	return S_OK;
}

ULONG STDMETHODCALLTYPE WASAPINotificationClient::AddRef() {
	return InterlockedIncrement(&_cRef);
}

ULONG STDMETHODCALLTYPE WASAPINotificationClient::Release() {
	// We hold a ref to ourselves all the time (static singleton) so no
	// need to clean ourselves up or anything.
	ULONG ulRef = InterlockedDecrement(&_cRef);
	Q_ASSERT(ulRef > 0);
	return ulRef;
}

void WASAPINotificationClient::enlistDefaultDeviceAsUsed(LPCWSTR pwstrDefaultDevice) {
	const QString device = QString::fromWCharArray(pwstrDefaultDevice);
	QMutexLocker lock(&listsMutex);
	if (!usedDefaultDevices.contains(device)) {
		usedDefaultDevices.append(device);
		_enlistDeviceAsUsed(device);
	}
}

void WASAPINotificationClient::enlistDeviceAsUsed(LPCWSTR pwstrDevice) {
	const QString device = QString::fromWCharArray(pwstrDevice);
	QMutexLocker lock(&listsMutex);
	_enlistDeviceAsUsed(device);
}

void WASAPINotificationClient::_enlistDeviceAsUsed(const QString &device) {
	if (!usedDevices.contains(device)) {
		usedDevices.append(device);
	}
}

void WASAPINotificationClient::enlistDeviceAsUsed(const QString &device) {
	QMutexLocker lock(&listsMutex);
	_enlistDeviceAsUsed(device);
}

void WASAPINotificationClient::unlistDevice(LPCWSTR pwstrDevice) {
	const QString device = QString::fromWCharArray(pwstrDevice);
	QMutexLocker lock(&listsMutex);
	usedDevices.removeOne(device);
	usedDefaultDevices.removeOne(device);
}

void WASAPINotificationClient::clearUsedDefaultDeviceList() {
	QMutexLocker lock(&listsMutex);
	usedDefaultDevices.clear();
}

void WASAPINotificationClient::_clearUsedDeviceLists() {
	usedDefaultDevices.clear();
	usedDevices.clear();
}

void WASAPINotificationClient::clearUsedDeviceLists() {
	QMutexLocker lock(&listsMutex);
	_clearUsedDeviceLists();
}

void WASAPINotificationClient::doGetOnce() {
	(void) WASAPINotificationClient::doGet();
}

WASAPINotificationClient &WASAPINotificationClient::doGet() {
	static WASAPINotificationClient instance;
	return instance;
}

static boost::once_flag notification_client_init_once = BOOST_ONCE_INIT;

WASAPINotificationClient &WASAPINotificationClient::get() {
	// Hacky way of making sure we get a thread-safe yet lazy initialization of the static.
	boost::call_once(&WASAPINotificationClient::doGetOnce, notification_client_init_once);
	return doGet();
}

WASAPINotificationClient::WASAPINotificationClient() : QObject(), pEnumerator(0), listsMutex() {
	AddRef(); // Static singleton, always has a self-reference

	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
								  reinterpret_cast< void ** >(&pEnumerator));
	if (!pEnumerator || FAILED(hr)) {
		if (pEnumerator) {
			pEnumerator->Release();
			pEnumerator = 0;
		}
		qWarning() << "WASAPINotificationClient: Failed to create enumerator, will not receive notifications";
		return;
	}

	Global::get().mw->connect(this, SIGNAL(doResetAudio()), SLOT(onResetAudio()), Qt::QueuedConnection);

	pEnumerator->RegisterEndpointNotificationCallback(this);
}

WASAPINotificationClient::~WASAPINotificationClient() {
	if (pEnumerator)
		pEnumerator->Release();
}

void WASAPINotificationClient::restartAudio() {
	qWarning("WASAPINotificationClient: Triggering audio reset");
	_clearUsedDeviceLists();
	emit doResetAudio();
}