-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasapiloopback.cpp
More file actions
176 lines (159 loc) · 5.13 KB
/
Copy pathwasapiloopback.cpp
File metadata and controls
176 lines (159 loc) · 5.13 KB
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
#include "wasapiloopback.h"
#include "wasapi-loopback/common.h"
QStringList WasapiLoopback::deviceNames()
{
QStringList names;
list_devices(names);
return names;
}
WasapiLoopback::WasapiLoopback(const QString name, QAudioFormat &format, QObject *parent) :
QIODevice(parent), deviceName(name), audioFormat(format)
{}
WasapiLoopback::~WasapiLoopback()
{
stop();
}
void WasapiLoopback::start(QIODevice *device)
{
if (!device) return;
stop();
if (state() != QAudio::StoppedState) return;
lastError = QAudio::NoError;
QAudioDeviceInfo outputDevice = QAudioDeviceInfo::defaultOutputDevice();
bool found = false;
if (!deviceName.isEmpty()) {
foreach (const QAudioDeviceInfo& dev, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) {
if (dev.deviceName() == deviceName) {
outputDevice = dev;
found = true;
break;
}
}
if (!found) {
foreach (const QAudioDeviceInfo& dev, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) {
QString name = dev.deviceName();
if (name == deviceName.left(name.length())) {
outputDevice = dev;
found = true;
break;
}
}
}
if (!found) qWarning() << "WasapiLoopback::start: output device not found";
}
audioOutput = new QAudioOutput(outputDevice, audioFormat, this);
audioOutput->setBufferSize(audioFormat.bytesForDuration(200000));
open(ReadWrite);
audioOutput->start(this);
bool error = false;
threadArgs = new LoopbackCaptureThreadFunctionArguments;
threadArgs->pMMDevice = NULL;
threadArgs->hStartedEvent = NULL;
threadArgs->hStopEvent = NULL;
threadArgs->hr = E_UNEXPECTED;
threadArgs->hThread = NULL;
threadArgs->ioDevice = device;
threadArgs->format = &audioFormat;
threadArgs->parent = this;
setState(QAudio::ActiveState);
if (!threadArgs) return;
if (!deviceName.isEmpty()) {
if (FAILED(get_specific_device(deviceName.toStdWString().c_str(), &threadArgs->pMMDevice))) {
threadArgs->pMMDevice = NULL;
}
}
if (!threadArgs->pMMDevice) {
if (FAILED(get_default_device(&threadArgs->pMMDevice))) {
threadArgs->pMMDevice = NULL;
error = true;
}
}
threadArgs->hStartedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (NULL == threadArgs->hStartedEvent) {
ERR(L"CreateEvent failed: last error is %u", GetLastError());
error = true;
}
threadArgs->hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (NULL == threadArgs->hStopEvent) {
ERR(L"CreateEvent failed: last error is %u", GetLastError());
error = true;
}
if (!error) {
threadArgs->hThread = CreateThread(
NULL, 0,
LoopbackCaptureThreadFunction, threadArgs,
0, NULL
);
if (NULL == threadArgs->hThread) {
ERR(L"CreateThread failed: last error is %u", GetLastError());
error = true;
}
}
if (error) {
lastError = QAudio::FatalError;
setState(QAudio::StoppedState);
}
}
void WasapiLoopback::stop()
{
if (audioOutput) {
audioOutput->stop();
audioOutput->deleteLater();
audioOutput = Q_NULLPTR;
close();
}
if (threadArgs) {
if (threadArgs->hThread) {
if (!SetEvent(threadArgs->hStopEvent)) {
ERR(L"SetEvent failed: last error is %d", GetLastError());
}
DWORD dwWaitResult = WaitForSingleObject(threadArgs->hThread, INFINITE);
if (WAIT_OBJECT_0 != dwWaitResult) {
ERR(L"WaitForSingleObject returned unexpected result 0x%08x, last error is %d", dwWaitResult, GetLastError());
}
}
if (threadArgs->pMMDevice) threadArgs->pMMDevice->Release();
CloseHandle(threadArgs->hStartedEvent);
CloseHandle(threadArgs->hStopEvent);
CloseHandle(threadArgs->hThread);
delete threadArgs;
threadArgs = Q_NULLPTR;
}
setState(QAudio::StoppedState);
}
QAudio::Error WasapiLoopback::error() const
{
QMutexLocker locker(&stateMutex);
return lastError;
}
QAudio::State WasapiLoopback::state() const
{
QMutexLocker locker(&stateMutex);
return currentState;
}
qint64 WasapiLoopback::readData(char *data, qint64 len)
{
memset(data, 0, len);
return len;
}
qint64 WasapiLoopback::writeData(const char *data, qint64 len)
{
return -1;
}
void WasapiLoopback::setState(QAudio::State newState)
{
QMutexLocker locker(&stateMutex);
if (newState != currentState) {
currentState = newState;
locker.unlock();
emit stateChanged(currentState);
}
}
void WasapiLoopback::onThreadEnd(QAudio::Error result)
{
{
QMutexLocker locker(&stateMutex);
lastError = result;
}
setState(QAudio::StoppedState);
}