-
Notifications
You must be signed in to change notification settings - Fork 226
/
WASAPICapture.cpp
536 lines (442 loc) · 15.7 KB
/
WASAPICapture.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//--------------------------------------------------------------------------------------
// WASAPICapture.cpp
//
// Class responsible for actually inputting samples to WASAPI.
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "WASAPICapture.h"
using namespace Microsoft::WRL;
using namespace Windows::Media::Devices;
//
// WASAPICapture()
//
WASAPICapture::WASAPICapture() :
m_BufferFrames( 0 ),
m_CaptureDeviceId ( 0 ),
m_UseLoopback ( false ),
m_SampleReadyEvent( INVALID_HANDLE_VALUE ),
m_CmdReadyEvent( INVALID_HANDLE_VALUE ),
m_MixFormat( nullptr ),
m_CaptureDevice( nullptr ),
m_AudioClient( nullptr ),
m_AudioCaptureClient( nullptr ),
m_DeviceStateChanged( nullptr )
{
// Create events for sample ready or user stop
m_SampleReadyEvent = CreateEventEx( nullptr, nullptr, 0, EVENT_ALL_ACCESS );
if (m_SampleReadyEvent)
{
if (InitializeCriticalSectionEx(&m_CritSec, 0, 0))
{
// Event, queue and Slim RW Lock for sending commands to the sample dispatch thread
m_CmdReadyEvent = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
if (m_CmdReadyEvent)
{
InitializeSRWLock(&m_CmdQueueLock);
m_DeviceStateChanged = ref new DeviceStateChangedEvent();
}
}
}
}
//
// ~WASAPICapture()
//
WASAPICapture::~WASAPICapture()
{
CoTaskMemFree( m_MixFormat );
if ( INVALID_HANDLE_VALUE != m_CmdReadyEvent )
{
CloseHandle( m_CmdReadyEvent );
m_CmdReadyEvent = INVALID_HANDLE_VALUE;
}
if (INVALID_HANDLE_VALUE != m_SampleReadyEvent)
{
CloseHandle( m_SampleReadyEvent );
m_SampleReadyEvent = INVALID_HANDLE_VALUE;
}
DeleteCriticalSection( &m_CritSec );
}
//--------------------------------------------------------------------------------------
// Name: Activate
// Desc: Callback implementation of ActivateAudioInterfaceAsync function.
// This will be called on MTA thread when results of the activation are available.
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::Activate(UINT id, bool bUseLoopback, CBuffer* capBuffer)
{
HRESULT hr = S_OK;
m_CaptureDeviceId = id;
if (m_DeviceStateChanged->GetState() != DeviceState::DeviceStateUnInitialized)
{
hr = E_NOT_VALID_STATE;
SetDeviceStateError( hr );
return hr;
}
m_DeviceStateChanged->SetState( DeviceState::DeviceStateActivated, S_OK, false );
ComPtr<IMMDeviceEnumerator> pEnumerator = nullptr;
ComPtr<IMMDeviceCollection> deviceCollectionInterface;
ComPtr<IMMDevice> pDevice = nullptr;
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), nullptr,
CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATEMASK_ALL, &deviceCollectionInterface);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
//Set the capture device based on the index
hr = deviceCollectionInterface->Item(m_CaptureDeviceId, &pDevice);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
hr = pDevice->Activate(
__uuidof(IAudioClient2), CLSCTX_ALL,
nullptr, (void**)&m_AudioClient);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
//Get the default format of the device
hr = m_AudioClient->GetMixFormat(&m_MixFormat);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
// Configure user defined properties
hr = ConfigureDeviceInternal();
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
// Initialize the AudioClient in Shared Mode with the user specified buffer
hr = m_AudioClient->Initialize( AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
REFTIMES_PER_SEC,
0,
m_MixFormat,
&AUDIOSESSIONGUID);
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
// Get the maximum size of the AudioClient Buffer
hr = m_AudioClient->GetBufferSize( &m_BufferFrames );
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
// Get the capture client
hr = m_AudioClient->GetService( __uuidof(IAudioCaptureClient), (void**)m_AudioCaptureClient.GetAddressOf() );
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
// Sets the event handle that the system signals when an audio buffer is ready to be processed by the client
hr = m_AudioClient->SetEventHandle( m_SampleReadyEvent );
if (FAILED( hr ))
{
SetDeviceStateError( hr );
return hr;
}
HANDLE hAudioSampleThread = CreateThread( nullptr, 0, AudioSampleThreadProc, this, 0, nullptr );
if ( nullptr == hAudioSampleThread )
{
hr = HRESULT_FROM_WIN32( GetLastError() );
if ( FAILED( hr ) )
SetDeviceStateError( hr );
return hr;
}
if ( !SetThreadPriority( hAudioSampleThread, THREAD_PRIORITY_TIME_CRITICAL ) )
{
hr = HRESULT_FROM_WIN32( GetLastError() );
if ( FAILED( hr ) )
SetDeviceStateError( hr );
return hr;
}
// Close thread handle if started successfully so thread can be destroyed when
// function exits.
CloseHandle( hAudioSampleThread );
// Everything succeeded
m_DeviceStateChanged->SetState( DeviceState::DeviceStateInitialized, S_OK, true );
m_UseLoopback = bUseLoopback;
m_captureBuffer = capBuffer;
// Need to return S_OK
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: ActivateCompleted
// Desc: Callback implementation of ActivateAudioInterfaceAsync function.
// This will be called on MTA thread when results of the activation are available.
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::ActivateCompleted( IActivateAudioInterfaceAsyncOperation *operation )
{
UNREFERENCED_PARAMETER(operation);
return S_OK;
}
void WASAPICapture::SetDeviceStateError( HRESULT hr )
{
m_DeviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
m_AudioClient = nullptr;
m_AudioCaptureClient = nullptr;
}
//--------------------------------------------------------------------------------------
// Name: ConfigureDeviceInternal
// Desc: Sets additional playback parameters.
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::ConfigureDeviceInternal()
{
if (m_DeviceStateChanged->GetState() != DeviceState::DeviceStateActivated)
{
return E_NOT_VALID_STATE;
}
HRESULT hr = S_OK;
// Hardware offload isn't supported
AudioClientProperties audioProps;
audioProps.cbSize = sizeof( AudioClientProperties );
audioProps.bIsOffload = false;
audioProps.eCategory = AudioCategory_ForegroundOnlyMedia;
hr = m_AudioClient->SetClientProperties( &audioProps );
if (FAILED( hr ))
{
return hr;
}
// This sample opens the device is shared mode so we need to find the supported WAVEFORMATEX mix format
hr = m_AudioClient->GetMixFormat( &m_MixFormat );
if (FAILED( hr ))
{
return hr;
}
return hr;
}
//--------------------------------------------------------------------------------------
// Name: StartCaptureAsync
// Desc: Starts asynchronous capture on a separate thread
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::StartCaptureAsync()
{
HRESULT hr = S_OK;
// We should be stopped if the user stopped playback, or we should be
// initialized if this is the first time through getting ready to playback.
if ( (m_DeviceStateChanged->GetState() == DeviceState::DeviceStateStopped) ||
(m_DeviceStateChanged->GetState() == DeviceState::DeviceStateInitialized) )
{
m_DeviceStateChanged->SetState( DeviceState::DeviceStateStarting, S_OK, true );
EnqueueCommand( CMD_START_CAPTURE );
return hr;
}
// Otherwise something else happened
return E_FAIL;
}
//--------------------------------------------------------------------------------------
// Name: StopCaptureAsync
// Desc: Stop playback asynchronously
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::StopCaptureAsync()
{
if ((m_DeviceStateChanged->GetState() != DeviceState::DeviceStateCapturing) &&
(m_DeviceStateChanged->GetState() != DeviceState::DeviceStateInError) )
{
return E_NOT_VALID_STATE;
}
m_DeviceStateChanged->SetState( DeviceState::DeviceStateStopping, S_OK, true );
EnqueueCommand( CMD_STOP_CAPTURE );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: OnStartCapture
// Desc: Method to start capture called from the high priority thread
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::OnStartCapture()
{
HRESULT hr = S_OK;
//Create the WAV file
m_pWaveFile = new CWaveFileWriter();
m_pWaveFile->Open(m_Filename, m_MixFormat);
// Actually start recording
hr = m_AudioClient->Start();
if ( SUCCEEDED( hr ) )
{
m_DeviceStateChanged->SetState( DeviceState::DeviceStateCapturing, S_OK, true );
}
else
{
m_DeviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: OnStopCapture
// Desc: Method to stop capture called from the high priority thread
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::OnStopCapture()
{
if(m_DeviceStateChanged->GetState() != DeviceState::DeviceStateCapturing)
{
// Flush anything left in buffer
OnAudioSampleRequested();
}
m_AudioClient->Stop();
m_pWaveFile->Close();
m_DeviceStateChanged->SetState( DeviceState::DeviceStateStopped, S_OK, true );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: SetCaptureDevice
// Desc: Sets the capture device
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::SetCaptureDevice(UINT id)
{
HRESULT hr = S_OK;
UINT iDeviceCount = 0;
ComPtr<IMMDeviceEnumerator> pEnumerator = nullptr;
ComPtr<IMMDeviceCollection> deviceCollectionInterface;
if(id == m_CaptureDeviceId)
{
return S_OK;
}
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), nullptr,
CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
//Check for valid ID
if ( SUCCEEDED( hr ) )
{
hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATEMASK_ALL, &deviceCollectionInterface);
}
if ( SUCCEEDED( hr ) )
{
hr = deviceCollectionInterface->GetCount(&iDeviceCount);
if ( FAILED( hr ) || iDeviceCount<=id)
{
return E_FAIL;
}
}
//Deactivate running device
m_CaptureDevice = nullptr;
//Activate new device
if ( SUCCEEDED( hr ) )
{
hr = Activate(id, m_UseLoopback, m_captureBuffer);
}
return hr;
}
//--------------------------------------------------------------------------------------
// Name: OnSampleReady
// Desc: Method called on high priority thread when ready to get a sample buffer
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::OnSampleReady()
{
HRESULT hr = S_OK;
hr = OnAudioSampleRequested();
if (FAILED( hr ))
{
m_DeviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
return hr;
}
//--------------------------------------------------------------------------------------
// Name: OnAudioSampleRequested
// Desc: Called when audio device fires m_SampleReadyEvent
//--------------------------------------------------------------------------------------
HRESULT WASAPICapture::OnAudioSampleRequested()
{
HRESULT hr = S_OK;
BYTE *captureData;
DWORD flags;
UINT32 numFramesAvailable;
// Prevent multiple concurrent submissions of samples
EnterCriticalSection( &m_CritSec );
hr = m_AudioCaptureClient->GetBuffer(&captureData, &numFramesAvailable, &flags, nullptr, nullptr);
if(SUCCEEDED( hr ))
{
//Write to WAV file
m_pWaveFile->WriteSample(captureData, numFramesAvailable * m_MixFormat->nBlockAlign * m_MixFormat->nChannels, nullptr);
if(m_UseLoopback)
{
//Copy data to CBuffer for the renderer
m_captureBuffer->SetCaptureBuffer(numFramesAvailable * m_MixFormat->nBlockAlign, captureData);
}
hr = m_AudioCaptureClient->ReleaseBuffer(numFramesAvailable);
}
LeaveCriticalSection( &m_CritSec );
return hr;
}
//--------------------------------------------------------------------------------------
// Name: AudioSampleThreadProc
// Desc: Shim for the AudioSampleThread method
//--------------------------------------------------------------------------------------
DWORD WINAPI WASAPICapture::AudioSampleThreadProc( LPVOID lpParameter )
{
DWORD result = S_OK;
result = ((WASAPICapture*)lpParameter)->AudioSampleThread();
return result;
}
//--------------------------------------------------------------------------------------
// Name: AudioSampleThread
// Desc: High priority thread that services samples and commands
//--------------------------------------------------------------------------------------
DWORD WASAPICapture::AudioSampleThread()
{
DWORD result = S_OK;
bool bQuit = false;
HANDLE eventHandles[] = { m_SampleReadyEvent, m_CmdReadyEvent };
while ( !bQuit )
{
DWORD reason = WaitForMultipleObjectsEx( _countof( eventHandles ), eventHandles, FALSE, INFINITE, TRUE );
if ( WAIT_OBJECT_0 == reason )
{
// Process a new set of samples
OnSampleReady();
}
else if ( ( WAIT_OBJECT_0 + 1 ) == reason )
{
// Commands are available in queue. Acquire a lock on the queue and
// copy commands into a local buffer so that we can process them
// without having to hold onto the lock.
AcquireSRWLockShared( &m_CmdQueueLock );
auto availCmds = m_CmdQueue.size();
std::vector<UINT32> cmds( availCmds );
for ( std::vector<UINT32>::size_type i = 0; i < availCmds; ++i )
{
cmds[ i ] = m_CmdQueue.front();
m_CmdQueue.pop_front();
}
ReleaseSRWLockShared( &m_CmdQueueLock );
// Now process the commands
for ( std::vector<UINT32>::size_type i = 0; i < availCmds; ++i )
{
switch ( cmds[ i ] )
{
case CMD_START_CAPTURE:
OnStartCapture();
break;
case CMD_STOP_CAPTURE:
OnStopCapture();
break;
}
}
}
else if ( WAIT_FAILED == reason )
{
bQuit = true;
}
}
return result;
}