-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathLoopbackCapture.cpp
More file actions
417 lines (341 loc) · 13.9 KB
/
LoopbackCapture.cpp
File metadata and controls
417 lines (341 loc) · 13.9 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <shlobj.h>
#include <wchar.h>
#include <iostream>
#include <audioclientactivationparams.h>
#include "LoopbackCapture.h"
#define BITS_PER_BYTE 8
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);
}
}
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 = 2;
m_CaptureFormat.nSamplesPerSec = 44100;
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 | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM,
200000,
0,
&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()));
// Creates the WAV file.
RETURN_IF_FAILED(CreateWAVFile());
// 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;
}
//
// CreateWAVFile()
//
// Creates a WAV file in music folder
//
HRESULT CLoopbackCapture::CreateWAVFile()
{
return SetDeviceStateErrorIfFailed([&]()->HRESULT
{
m_hFile.reset(CreateFile(m_outputFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));
RETURN_LAST_ERROR_IF(!m_hFile);
// Create and write the WAV header
// 1. RIFF chunk descriptor
DWORD header[] = {
FCC('RIFF'), // RIFF header
0, // Total size of WAV (will be filled in later)
FCC('WAVE'), // WAVE FourCC
FCC('fmt '), // Start of 'fmt ' chunk
sizeof(m_CaptureFormat) // Size of fmt chunk
};
DWORD dwBytesWritten = 0;
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(m_hFile.get(), header, sizeof(header), &dwBytesWritten, NULL));
m_cbHeaderSize += dwBytesWritten;
// 2. The fmt sub-chunk
WI_ASSERT(m_CaptureFormat.cbSize == 0);
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(m_hFile.get(), &m_CaptureFormat, sizeof(m_CaptureFormat), &dwBytesWritten, NULL));
m_cbHeaderSize += dwBytesWritten;
// 3. The data sub-chunk
DWORD data[] = { FCC('data'), 0 }; // Start of 'data' chunk
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(m_hFile.get(), data, sizeof(data), &dwBytesWritten, NULL));
m_cbHeaderSize += dwBytesWritten;
return S_OK;
}());
}
//
// FixWAVHeader()
//
// The size values were not known when we originally wrote the header, so now go through and fix the values
//
HRESULT CLoopbackCapture::FixWAVHeader()
{
// Write the size of the 'data' chunk first
DWORD dwPtr = SetFilePointer(m_hFile.get(), m_cbHeaderSize - sizeof(DWORD), NULL, FILE_BEGIN);
RETURN_LAST_ERROR_IF(INVALID_SET_FILE_POINTER == dwPtr);
DWORD dwBytesWritten = 0;
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(m_hFile.get(), &m_cbDataSize, sizeof(DWORD), &dwBytesWritten, NULL));
// Write the total file size, minus RIFF chunk and size
// sizeof(DWORD) == sizeof(FOURCC)
RETURN_LAST_ERROR_IF(INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile.get(), sizeof(DWORD), NULL, FILE_BEGIN));
DWORD cbTotalSize = m_cbDataSize + m_cbHeaderSize - 8;
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(m_hFile.get(), &cbTotalSize, sizeof(DWORD), &dwBytesWritten, NULL));
RETURN_IF_WIN32_BOOL_FALSE(FlushFileBuffers(m_hFile.get()));
return S_OK;
}
HRESULT CLoopbackCapture::StartCaptureAsync(DWORD processId, bool includeProcessTree, PCWSTR outputFileName)
{
m_outputFileName = outputFileName;
auto resetOutputFileName = wil::scope_exit([&] { m_outputFileName = nullptr; });
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 S_OK;
}
//
// 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)
{
// FixWAVHeader will set the DeviceStateStopped when all async tasks are complete
HRESULT hr = FixWAVHeader();
m_DeviceState = DeviceState::Stopped;
m_hCaptureStopped.SetEvent();
return hr;
}
//
// 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;
// WAV files have a 4GB (0xFFFFFFFF) size limit, so likely we have hit that limit when we
// overflow here. Time to stop the capture
if ((m_cbDataSize + cbBytesToCapture) < m_cbDataSize)
{
StopCaptureAsync();
break;
}
// Get sample buffer
RETURN_IF_FAILED(m_AudioCaptureClient->GetBuffer(&Data, &FramesAvailable, &dwCaptureFlags, &u64DevicePosition, &u64QPCPosition));
// Write File
if (m_DeviceState != DeviceState::Stopping)
{
DWORD dwBytesWritten = 0;
RETURN_IF_WIN32_BOOL_FALSE(WriteFile(
m_hFile.get(),
Data,
cbBytesToCapture,
&dwBytesWritten,
NULL));
}
// Release buffer back
m_AudioCaptureClient->ReleaseBuffer(FramesAvailable);
// Increase the size of our 'data' chunk. m_cbDataSize needs to be accurate
m_cbDataSize += cbBytesToCapture;
}
return S_OK;
}