-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaptureWrapper.h
More file actions
354 lines (275 loc) · 10.4 KB
/
Copy pathCaptureWrapper.h
File metadata and controls
354 lines (275 loc) · 10.4 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// intended to be a cross platform way to do calls to stuff
#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <locale>
#include <codecvt>
#include <iostream>
#if WIN32
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <audioclient.h>
#include <comdef.h>
#include <Psapi.h>
#include <avrt.h>
#include "./windows/audiocapture.h"
#endif
#if MACOSX
#include "audiocapture.h"
#endif
#include "UsefulTypes.h"
// OO YEA I LOVE PIMPLS
class AudioCapture {
public:
inline AudioCapture();
~AudioCapture() = default;
void registerAudioBufferCallback(callbackFunction functionPointer) {
impl->registerAudioBufferCallback(functionPointer);
}
void startAudioCapture() {
impl->startAudioCapture();
hasStartedAtLeastOnce = true;
}
void stopAudioCapture() {
if (hasStartedAtLeastOnce) {
impl->stopAudioCapture();
}
}
std::vector<AppStruct> getValidWindows() {
auto validWindows = impl->getValidWindows();
// Remove the app with the name ${JUCE_APPLICATION_NAME_STRING} from the list
auto it = std::remove_if(validWindows.begin(), validWindows.end(),
[](const AppStruct& app) {
return app.applicationName == JUCE_APPLICATION_NAME_STRING;
});
validWindows.erase(it, validWindows.end());
return validWindows;
}
void setActiveProcess(AppStruct app) {
impl->stopAudioCapture();
impl->setActiveProcess(app);
impl->startAudioCapture();
}
class ImplBase {
public:
// virtual ~ImplBase() = default;
virtual void startAudioCapture() = 0;
virtual void stopAudioCapture() = 0;
virtual void registerAudioBufferCallback(callbackFunction functionPointer) = 0;
virtual std::vector<AppStruct> getValidWindows() = 0;
virtual void setActiveProcess(AppStruct app) = 0;
ValidAppsStruct validApps;
protected:
callbackFunction audioCallback;
private:
};
std::unique_ptr<ImplBase> impl;
private:
bool hasStartedAtLeastOnce = false;
};
// specific impl for macos, which uses screencapturekit
#ifdef MACOSX
class MacAudioCapture : public AudioCapture::ImplBase {
public:
MacAudioCapture() {
std::cout << "MacAudioCapture constructor" << std::endl;
currentActiveApp.processID = -1;
sckAudioCapture.registerAudioBufferCallback(audioCallback);
}
~MacAudioCapture() {
std::cout << "MacAudioCapture destructor" << std::endl;
}
void startAudioCapture() {
std::cout << "MacAudioCapture startAudioCapture" << std::endl;
readerThread = std::thread([&]() {
sckAudioCapture.startAudioCapture(currentActiveApp.processID, 1);
});
}
void stopAudioCapture() {
std::cout << "MacAudioCapture stopAudioCapture" << std::endl;
sckAudioCapture.stopAudioCapture();
if (readerThread.joinable())
readerThread.join();
// readerThread = std::thread([&]() {
// sckAudioCapture.startAudioCapture(currentActiveApp.processID, 1);
// });
// sckAudioCapture.startAudioCapture();
}
void registerAudioBufferCallback(callbackFunction functionPointer) {
std::cout << "MacAudioCapture registerAudioBufferCallback" << std::endl;
audioCallback = functionPointer;
sckAudioCapture.registerAudioBufferCallback(audioCallback);
}
std::vector<AppStruct> getValidWindows() {
std::vector<AppStruct> validWindows;
auto resp = sckAudioCapture.getValidWindows();
AppStruct allProcesses;
allProcesses.processID = -1;
allProcesses.bundleIdentifier = "allProcesses";
allProcesses.applicationName = "All Processes";
validWindows.clear();
validWindows.push_back(allProcesses);
for (int i = 1; i < resp.size; i++) {
validWindows.push_back((AppStruct)resp.apps[i]);
std::cout << resp.apps[i].applicationName << ", " << resp.apps[i].bundleIdentifier << ", " << resp.apps[i].processID << std::endl;
}
return validWindows;
}
void setActiveProcess(AppStruct app) {
currentActiveApp = app;
}
private:
SCKAudioCapture sckAudioCapture;
AppStruct currentActiveApp;
std::thread readerThread;
};
#endif
// specific impl for windows, which uses wasapi?
// specific impl for macos, which uses screencapturekit
#ifdef WIN32
class WindowsAudioCapture : public AudioCapture::ImplBase {
public:
WindowsAudioCapture() {
std::cout << "WindowsAudioCapture constructor" << std::endl;
currentActiveApp.processID = -1;
}
~WindowsAudioCapture() {
std::cout << "WindowsAudioCapture destructor" << std::endl;
}
void startAudioCapture() {
std::cout << "WindowsAudioCapture startAudioCapture" << std::endl;
if (currentActiveApp.processID == -1) {
std::cerr << "No active process set, include everything" << std::endl;
loopbackCapture.StartCaptureAsync(-1, false);
} else {
loopbackCapture.StartCaptureAsync(currentActiveApp.processID, true);
}
}
void stopAudioCapture() {
std::cout << "WindowsAudioCapture stopAudioCapture" << std::endl;
loopbackCapture.StopCaptureAsync();
}
void registerAudioBufferCallback(callbackFunction functionPointer) {
std::cout << "WindowsAudioCapture registerAudioBufferCallback" << std::endl;
audioCallback = functionPointer;
loopbackCapture.registerAudioCallback(audioCallback);
}
std::vector<AppStruct> getValidWindows() {
// HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
// if (FAILED(hr))
// {
// std::cerr << "Failed to initialize COM library." << std::endl;
// return std::vector<AppStruct>();
// }
IMMDeviceEnumerator *pDeviceEnumerator = nullptr;
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pDeviceEnumerator));
if (FAILED(hr))
{
std::cerr << "Failed to create device enumerator." << std::endl;
CoUninitialize();
return std::vector<AppStruct>();
}
IMMDevice *pDefaultDevice = nullptr;
hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultDevice);
if (FAILED(hr))
{
std::cerr << "Failed to get default audio endpoint." << std::endl;
pDeviceEnumerator->Release();
CoUninitialize();
return std::vector<AppStruct>();
}
IAudioSessionManager2 *pAudioSessionManager = nullptr;
hr = pDefaultDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, nullptr, (void **)&pAudioSessionManager);
if (FAILED(hr))
{
std::cerr << "Failed to activate audio session manager." << std::endl;
pDefaultDevice->Release();
pDeviceEnumerator->Release();
CoUninitialize();
return std::vector<AppStruct>();
}
IAudioSessionEnumerator *pSessionEnumerator = nullptr;
hr = pAudioSessionManager->GetSessionEnumerator(&pSessionEnumerator);
if (FAILED(hr))
{
std::cerr << "Failed to get session enumerator." << std::endl;
pAudioSessionManager->Release();
pDefaultDevice->Release();
pDeviceEnumerator->Release();
CoUninitialize();
return std::vector<AppStruct>();
}
int sessionCount = 0;
hr = pSessionEnumerator->GetCount(&sessionCount);
validWindows.clear();
if (SUCCEEDED(hr))
{
for (int i = 0; i < sessionCount; ++i)
{
IAudioSessionControl *pSessionControl = nullptr;
hr = pSessionEnumerator->GetSession(i, &pSessionControl);
if (SUCCEEDED(hr))
{
IAudioSessionControl2 *pSessionControl2;
hr = pSessionControl->QueryInterface(IID_PPV_ARGS(&pSessionControl2));
if (SUCCEEDED(hr))
{
DWORD processId = 0;
hr = pSessionControl2->GetProcessId(&processId);
if (SUCCEEDED(hr))
{
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (processHandle)
{
WCHAR processName[MAX_PATH];
if (GetModuleBaseNameW(processHandle, nullptr, processName, MAX_PATH) > 0)
{
AppStruct app;
std::wstring ws( processName );
std::string appName( ws.begin(), ws.end() );
app.applicationName = appName;
app.processID = processId;
validWindows.push_back(app);
}
CloseHandle(processHandle);
}
}
pSessionControl2->Release();
}
pSessionControl->Release();
}
}
}
AppStruct allProcesses;
allProcesses.processID = -1;
allProcesses.bundleIdentifier = "allProcesses";
allProcesses.applicationName = "All Processes";
validWindows.push_back(allProcesses);
pSessionEnumerator->Release();
pAudioSessionManager->Release();
pDefaultDevice->Release();
pDeviceEnumerator->Release();
CoUninitialize();
return validWindows;
}
void setActiveProcess(AppStruct app) {
currentActiveApp = app;
}
private:
std::vector<AppStruct> validWindows;
AppStruct currentActiveApp;
std::thread readerThread;
CLoopbackCapture loopbackCapture;
};
#endif
AudioCapture::AudioCapture() {
#if WIN32
impl = std::make_unique<WindowsAudioCapture>();
#endif
#if MACOSX
impl = std::make_unique<MacAudioCapture>();
#endif
};