-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLoopbackCapture.cpp
369 lines (307 loc) · 12.2 KB
/
LoopbackCapture.cpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "pch.h"
#include <shlobj.h>
#include <wchar.h>
#include <iostream>
#include "LoopbackCapture.h"
#define BITS_PER_BYTE 8
// Identifier for virtual audio device that supports audio loopback based on
// a process ID instead of the device interface path of a physical audio device.
// Use this for the deviceInterfacePath parameter of ActivateAudioInterfaceAsync when
// AUDIOCLIENT_ACTIVATION_PARAMS::ActivationType is set to AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK.
#define VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK L"VAD\\Process_Loopback"
typedef /* [v1_enum] */
enum PROCESS_LOOPBACK_MODE
{
PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE = 0,
PROCESS_LOOPBACK_MODE_EXCLUDE_TARGET_PROCESS_TREE = 1
} PROCESS_LOOPBACK_MODE;
// This structure is used when creating an IAudioClient using ActivateAudioInterfaceAsync
// for process-based loopback capture. The captured audio either includes or excludes audio rendered
// by the specified process and its child processes, based on how the ProcessLoopbackMode field is set.
typedef struct AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS
{
DWORD TargetProcessId;
PROCESS_LOOPBACK_MODE ProcessLoopbackMode;
} AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS;
typedef /* [v1_enum] */
enum AUDIOCLIENT_ACTIVATION_TYPE
{
AUDIOCLIENT_ACTIVATION_TYPE_DEFAULT = 0,
AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK = 1
} AUDIOCLIENT_ACTIVATION_TYPE;
// Activation parameter structure that can be used with ActivateAudioInterfaceAsync
// to create an IAudioClient.
typedef struct AUDIOCLIENT_ACTIVATION_PARAMS
{
AUDIOCLIENT_ACTIVATION_TYPE ActivationType;
union
{
AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS ProcessLoopbackParams;
} DUMMYUNIONNAME;
} AUDIOCLIENT_ACTIVATION_PARAMS;
HRESULT CLoopbackCapture::SetDeviceStateErrorIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
m_DeviceState = DeviceState::Error;
}
return hr;
}
HRESULT CLoopbackCapture::InitializeLoopbackCapture()
{
// Create events for sample ready or user stop
RETURN_IF_FAILED(m_SampleReadyEvent.create(wil::EventOptions::None));
// Initialize MF
RETURN_IF_FAILED(MFStartup(MF_VERSION, MFSTARTUP_LITE));
// Register MMCSS work queue
DWORD dwTaskID = 0;
RETURN_IF_FAILED(MFLockSharedWorkQueue(L"Capture", 0, &dwTaskID, &m_dwQueueID));
// Set the capture event work queue to use the MMCSS queue
m_xSampleReady.SetQueueID(m_dwQueueID);
// Create the completion event as auto-reset
RETURN_IF_FAILED(m_hActivateCompleted.create(wil::EventOptions::None));
// Create the capture-stopped event as auto-reset
RETURN_IF_FAILED(m_hCaptureStopped.create(wil::EventOptions::None));
return S_OK;
}
CLoopbackCapture::~CLoopbackCapture()
{
if (m_dwQueueID != 0)
{
MFUnlockWorkQueue(m_dwQueueID);
}
}
void CLoopbackCapture::SetOutputSink(std::function<void(std::vector<uint8_t>&&)> samples)
{
m_samples = samples;
}
HRESULT CLoopbackCapture::ActivateAudioInterface(DWORD processId, bool includeProcessTree)
{
return SetDeviceStateErrorIfFailed([&]() -> HRESULT
{
AUDIOCLIENT_ACTIVATION_PARAMS audioclientActivationParams = {};
audioclientActivationParams.ActivationType = AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK;
audioclientActivationParams.ProcessLoopbackParams.ProcessLoopbackMode = includeProcessTree ?
PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE : PROCESS_LOOPBACK_MODE_EXCLUDE_TARGET_PROCESS_TREE;
audioclientActivationParams.ProcessLoopbackParams.TargetProcessId = processId;
PROPVARIANT activateParams = {};
activateParams.vt = VT_BLOB;
activateParams.blob.cbSize = sizeof(audioclientActivationParams);
activateParams.blob.pBlobData = (BYTE*)&audioclientActivationParams;
wil::com_ptr_nothrow<IActivateAudioInterfaceAsyncOperation> asyncOp;
RETURN_IF_FAILED(ActivateAudioInterfaceAsync(VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, __uuidof(IAudioClient), &activateParams, this, &asyncOp));
// Wait for activation completion
m_hActivateCompleted.wait();
return m_activateResult;
}());
}
//
// ActivateCompleted()
//
// Callback implementation of ActivateAudioInterfaceAsync function. This will be called on MTA thread
// when results of the activation are available.
//
HRESULT CLoopbackCapture::ActivateCompleted(IActivateAudioInterfaceAsyncOperation* operation)
{
m_activateResult = SetDeviceStateErrorIfFailed([&]()->HRESULT
{
// Check for a successful activation result
HRESULT hrActivateResult = E_UNEXPECTED;
wil::com_ptr_nothrow<IUnknown> punkAudioInterface;
RETURN_IF_FAILED(operation->GetActivateResult(&hrActivateResult, &punkAudioInterface));
RETURN_IF_FAILED(hrActivateResult);
// Get the pointer for the Audio Client
RETURN_IF_FAILED(punkAudioInterface.copy_to(&m_AudioClient));
// The app can also call m_AudioClient->GetMixFormat instead to get the capture format.
// 16 - bit PCM format.
m_CaptureFormat.wFormatTag = WAVE_FORMAT_PCM;
m_CaptureFormat.nChannels = 1;
m_CaptureFormat.nSamplesPerSec = 48000;
m_CaptureFormat.wBitsPerSample = 16;
m_CaptureFormat.nBlockAlign = m_CaptureFormat.nChannels * m_CaptureFormat.wBitsPerSample / BITS_PER_BYTE;
m_CaptureFormat.nAvgBytesPerSec = m_CaptureFormat.nSamplesPerSec * m_CaptureFormat.nBlockAlign;
// Initialize the AudioClient in Shared Mode with the user specified buffer
RETURN_IF_FAILED(m_AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
200000,
AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM,
&m_CaptureFormat,
nullptr));
// Get the maximum size of the AudioClient Buffer
RETURN_IF_FAILED(m_AudioClient->GetBufferSize(&m_BufferFrames));
// Get the capture client
RETURN_IF_FAILED(m_AudioClient->GetService(IID_PPV_ARGS(&m_AudioCaptureClient)));
// Create Async callback for sample events
RETURN_IF_FAILED(MFCreateAsyncResult(nullptr, &m_xSampleReady, nullptr, &m_SampleReadyAsyncResult));
// Tell the system which event handle it should signal when an audio buffer is ready to be processed by the client
RETURN_IF_FAILED(m_AudioClient->SetEventHandle(m_SampleReadyEvent.get()));
// Everything is ready.
m_DeviceState = DeviceState::Initialized;
return S_OK;
}());
// Let ActivateAudioInterface know that m_activateResult has the result of the activation attempt.
m_hActivateCompleted.SetEvent();
return S_OK;
}
HRESULT CLoopbackCapture::StartCaptureAsync(DWORD processId, bool includeProcessTree)
{
RETURN_IF_FAILED(InitializeLoopbackCapture());
RETURN_IF_FAILED(ActivateAudioInterface(processId, includeProcessTree));
// We should be in the initialzied state if this is the first time through getting ready to capture.
if (m_DeviceState == DeviceState::Initialized)
{
m_DeviceState = DeviceState::Starting;
return MFPutWorkItem2(MFASYNC_CALLBACK_QUEUE_MULTITHREADED, 0, &m_xStartCapture, nullptr);
}
return S_OK;
}
//
// OnStartCapture()
//
// Callback method to start capture
//
HRESULT CLoopbackCapture::OnStartCapture(IMFAsyncResult* pResult)
{
return SetDeviceStateErrorIfFailed([&]()->HRESULT
{
// Start the capture
RETURN_IF_FAILED(m_AudioClient->Start());
m_DeviceState = DeviceState::Capturing;
MFPutWaitingWorkItem(m_SampleReadyEvent.get(), 0, m_SampleReadyAsyncResult.get(), &m_SampleReadyKey);
return S_OK;
}());
}
//
// StopCaptureAsync()
//
// Stop capture asynchronously via MF Work Item
//
HRESULT CLoopbackCapture::StopCaptureAsync()
{
RETURN_HR_IF(E_NOT_VALID_STATE, (m_DeviceState != DeviceState::Capturing) &&
(m_DeviceState != DeviceState::Error));
m_DeviceState = DeviceState::Stopping;
RETURN_IF_FAILED(MFPutWorkItem2(MFASYNC_CALLBACK_QUEUE_MULTITHREADED, 0, &m_xStopCapture, nullptr));
// Wait for capture to stop
m_hCaptureStopped.wait();
return MFShutdown();
}
//
// OnStopCapture()
//
// Callback method to stop capture
//
HRESULT CLoopbackCapture::OnStopCapture(IMFAsyncResult* pResult)
{
// Stop capture by cancelling Work Item
// Cancel the queued work item (if any)
if (0 != m_SampleReadyKey)
{
MFCancelWorkItem(m_SampleReadyKey);
m_SampleReadyKey = 0;
}
m_AudioClient->Stop();
m_SampleReadyAsyncResult.reset();
return FinishCaptureAsync();
}
//
// FinishCaptureAsync()
//
// Finalizes WAV file on a separate thread via MF Work Item
//
HRESULT CLoopbackCapture::FinishCaptureAsync()
{
// We should be flushing when this is called
return MFPutWorkItem2(MFASYNC_CALLBACK_QUEUE_MULTITHREADED, 0, &m_xFinishCapture, nullptr);
}
//
// OnFinishCapture()
//
// Because of the asynchronous nature of the MF Work Queues and the DataWriter, there could still be
// a sample processing. So this will get called to finalize the WAV header.
//
HRESULT CLoopbackCapture::OnFinishCapture(IMFAsyncResult* pResult)
{
m_DeviceState = DeviceState::Stopped;
m_hCaptureStopped.SetEvent();
return S_OK;
}
//
// OnSampleReady()
//
// Callback method when ready to fill sample buffer
//
HRESULT CLoopbackCapture::OnSampleReady(IMFAsyncResult* pResult)
{
if (SUCCEEDED(OnAudioSampleRequested()))
{
// Re-queue work item for next sample
if (m_DeviceState == DeviceState::Capturing)
{
// Re-queue work item for next sample
return MFPutWaitingWorkItem(m_SampleReadyEvent.get(), 0, m_SampleReadyAsyncResult.get(), &m_SampleReadyKey);
}
}
else
{
m_DeviceState = DeviceState::Error;
}
return S_OK;
}
//
// OnAudioSampleRequested()
//
// Called when audio device fires m_SampleReadyEvent
//
HRESULT CLoopbackCapture::OnAudioSampleRequested()
{
UINT32 FramesAvailable = 0;
BYTE* Data = nullptr;
DWORD dwCaptureFlags;
UINT64 u64DevicePosition = 0;
UINT64 u64QPCPosition = 0;
DWORD cbBytesToCapture = 0;
auto lock = m_CritSec.lock();
// If this flag is set, we have already queued up the async call to finialize the WAV header
// So we don't want to grab or write any more data that would possibly give us an invalid size
if (m_DeviceState == DeviceState::Stopping)
{
return S_OK;
}
// A word on why we have a loop here;
// Suppose it has been 10 milliseconds or so since the last time
// this routine was invoked, and that we're capturing 48000 samples per second.
//
// The audio engine can be reasonably expected to have accumulated about that much
// audio data - that is, about 480 samples.
//
// However, the audio engine is free to accumulate this in various ways:
// a. as a single packet of 480 samples, OR
// b. as a packet of 80 samples plus a packet of 400 samples, OR
// c. as 48 packets of 10 samples each.
//
// In particular, there is no guarantee that this routine will be
// run once for each packet.
//
// So every time this routine runs, we need to read ALL the packets
// that are now available;
//
// We do this by calling IAudioCaptureClient::GetNextPacketSize
// over and over again until it indicates there are no more packets remaining.
while (SUCCEEDED(m_AudioCaptureClient->GetNextPacketSize(&FramesAvailable)) && FramesAvailable > 0)
{
cbBytesToCapture = FramesAvailable * m_CaptureFormat.nBlockAlign;
// Get sample buffer
RETURN_IF_FAILED(m_AudioCaptureClient->GetBuffer(&Data, &FramesAvailable, &dwCaptureFlags, &u64DevicePosition, &u64QPCPosition));
// Write File
if (m_DeviceState != DeviceState::Stopping)
{
std::vector<uint8_t> bytes(cbBytesToCapture);
memcpy(bytes.data(), Data, cbBytesToCapture);
m_samples(std::move(bytes));
}
// Release buffer back
m_AudioCaptureClient->ReleaseBuffer(FramesAvailable);
}
return S_OK;
}