-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathWASAPICapture.cpp
356 lines (302 loc) · 10 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
//--------------------------------------------------------------------------------------
// WASAPICapture.cpp
//
// Functions to assist in capturing audio in WASAPI
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "WASAPICapture.h"
using namespace Windows::System::Threading;
//
// WASAPICapture()
//
WASAPICapture::WASAPICapture() :
m_bufferFrames( 0 ),
m_queueID( 0 ),
m_deviceStateChanged( nullptr ),
m_audioClient( nullptr ),
m_audioCaptureClient( nullptr ),
m_sampleReadyAsyncResult( nullptr ),
m_resampler( nullptr ),
m_buffer( nullptr )
{
// Create events for sample ready or user stop
m_sampleReadyEvent = CreateEventEx( nullptr, nullptr, 0, EVENT_ALL_ACCESS );
if (nullptr == m_sampleReadyEvent)
{
DX::ThrowIfFailed( HRESULT_FROM_WIN32( GetLastError() ) );
}
m_deviceStateChanged = ref new DeviceStateChangedEvent();
if (nullptr == m_deviceStateChanged)
{
DX::ThrowIfFailed( E_OUTOFMEMORY );
}
// Register MMCSS work queue
HRESULT hr = S_OK;
DWORD taskID = 0;
hr = MFLockSharedWorkQueue( L"Capture", 0, &taskID, &m_queueID );
if (FAILED( hr ))
{
DX::ThrowIfFailed( hr );
}
// Set the capture event work queue to use the MMCSS queue
m_xSampleReady.SetQueueID(m_queueID);
}
//
// ~WASAPICapture()
//
WASAPICapture::~WASAPICapture()
{
if (INVALID_HANDLE_VALUE != m_sampleReadyEvent)
{
CloseHandle( m_sampleReadyEvent );
m_sampleReadyEvent = INVALID_HANDLE_VALUE;
}
MFUnlockWorkQueue( m_queueID );
m_deviceStateChanged = nullptr;
}
//
// InitializeAudioDeviceAsync()
//
// Activates the default audio capture on a asynchronous callback thread. This needs
// to be called from the main UI thread.
//
HRESULT WASAPICapture::InitializeAudioDeviceAsync(Platform::String^ deviceIdString)
{
IActivateAudioInterfaceAsyncOperation *asyncOp;
HRESULT hr = S_OK;
// This call must be made on the main UI thread. Async operation will call back to
// IActivateAudioInterfaceCompletionHandler::ActivateCompleted, which must be an agile interface implementation
hr = ActivateAudioInterfaceAsync(deviceIdString->Data(), __uuidof(IAudioClient2), nullptr, this, &asyncOp );
if (FAILED( hr ))
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
SAFE_RELEASE( asyncOp );
return hr;
}
//
// ActivateCompleted()
//
// Callback implementation of ActivateAudioInterfaceAsync function. This will be called on MTA thread
// when results of the activation are available.
//
HRESULT WASAPICapture::ActivateCompleted( IActivateAudioInterfaceAsyncOperation *operation )
{
HRESULT hr = S_OK;
HRESULT hrActivateResult = S_OK;
Microsoft::WRL::ComPtr<IUnknown> punkAudioInterface = nullptr;
// Check for a successful activation result
hr = operation->GetActivateResult( &hrActivateResult, &punkAudioInterface );
if (SUCCEEDED( hr ) && SUCCEEDED( hrActivateResult ))
{
// Get the pointer for the Audio Client
punkAudioInterface.Get()->QueryInterface( IID_PPV_ARGS(&m_audioClient) );
if (nullptr == m_audioClient)
{
hr = E_FAIL;
goto exit;
}
hr = m_audioClient->GetMixFormat( &m_mixFormat );
if (FAILED( hr ))
{
goto exit;
}
// Initialize the AudioClient in Shared Mode with the user specified buffer
hr = m_audioClient->Initialize( AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
10000000LL,
0,
m_mixFormat,
&AUDIOSESSIONGUID);
if (FAILED( hr ))
{
goto exit;
}
// Get the maximum size of the AudioClient Buffer
hr = m_audioClient->GetBufferSize( &m_bufferFrames );
if (FAILED( hr ))
{
goto exit;
}
// Get the capture client
hr = m_audioClient->GetService( __uuidof(IAudioCaptureClient), (void**) &m_audioCaptureClient );
if (FAILED( hr ))
{
goto exit;
}
// Create Async callback for sample events
hr = MFCreateAsyncResult( nullptr, &m_xSampleReady, nullptr, &m_sampleReadyAsyncResult );
if (FAILED( hr ))
{
goto exit;
}
// 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 ))
{
goto exit;
}
m_deviceStateChanged->SetState(DeviceState::DeviceStateInitialized, S_OK, true);
}
exit:
if (FAILED( hr ))
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
// Need to return S_OK
return S_OK;
}
//
// StartCaptureAsync()
//
// Starts asynchronous capture on a separate thread via MF Work Item
//
HRESULT WASAPICapture::StartCaptureAsync(CBuffer* bufferToUse)
{
m_buffer = bufferToUse;
// We should be in the initialzied state if this is the first time through getting ready to capture.
if (m_deviceStateChanged->GetState() == DeviceState::DeviceStateInitialized)
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateStarting, S_OK, true );
return MFPutWorkItem2( MFASYNC_CALLBACK_QUEUE_MULTITHREADED, 0, &m_xStartCapture, nullptr );
}
// We are in the wrong state
return E_NOT_VALID_STATE;
}
//
// OnStartCapture()
//
// Callback method to start capture
//
HRESULT WASAPICapture::OnStartCapture( IMFAsyncResult* )
{
HRESULT hr = S_OK;
// Start the capture
hr = m_audioClient->Start();
if (SUCCEEDED( hr ))
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateCapturing, S_OK, true );
MFPutWaitingWorkItem( m_sampleReadyEvent, 0, m_sampleReadyAsyncResult.Get(), &m_sampleReadyKey );
}
else
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
return S_OK;
}
//
// StopCaptureAsync()
//
// Stop capture asynchronously via MF Work Item
//
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 );
return MFPutWorkItem2( MFASYNC_CALLBACK_QUEUE_MULTITHREADED, 0, &m_xStopCapture, nullptr );
}
//
// OnStopCapture()
//
// Callback method to stop capture
//
HRESULT WASAPICapture::OnStopCapture( IMFAsyncResult* )
{
// 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_deviceStateChanged->SetState( DeviceState::DeviceStateFlushing, S_OK, true );
m_buffer = nullptr;
return S_OK;
}
//
// OnSampleReady()
//
// Callback method when ready to fill sample buffer
//
HRESULT WASAPICapture::OnSampleReady( IMFAsyncResult* )
{
HRESULT hr = S_OK;
hr = OnAudioSampleRequested( false );
if (SUCCEEDED( hr ))
{
// Re-queue work item for next sample
if (m_deviceStateChanged->GetState() == DeviceState::DeviceStateCapturing)
{
hr = MFPutWaitingWorkItem( m_sampleReadyEvent, 0, m_sampleReadyAsyncResult.Get(), &m_sampleReadyKey );
}
}
else
{
m_deviceStateChanged->SetState( DeviceState::DeviceStateInError, hr, true );
}
return hr;
}
//
// OnAudioSampleRequested()
//
// Called when audio device fires m_sampleReadyEvent
//
HRESULT WASAPICapture::OnAudioSampleRequested( bool isSilence )
{
HRESULT hr = S_OK;
uint32_t framesAvailable = 0;
BYTE *data = nullptr;
DWORD captureFlags;
UINT64 devicePosition = 0;
UINT64 QPCPosition = 0;
DWORD bytesToCapture = 0;
std::lock_guard<std::mutex> lck(m_mutex);
// 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_deviceStateChanged->GetState() == DeviceState::DeviceStateStopping) ||
(m_deviceStateChanged->GetState() == DeviceState::DeviceStateFlushing) )
{
goto exit;
}
// This should equal the buffer size when GetBuffer() is called
hr = m_audioCaptureClient->GetNextPacketSize( &framesAvailable);
if (FAILED( hr ))
{
goto exit;
}
if (framesAvailable > 0)
{
bytesToCapture = framesAvailable * m_mixFormat->nBlockAlign;
// Get sample buffer
hr = m_audioCaptureClient->GetBuffer( &data, &framesAvailable, &captureFlags, &devicePosition, &QPCPosition );
if (FAILED( hr ))
{
goto exit;
}
if (captureFlags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
{
// Pass down a discontinuity flag in case the app is interested and reset back to capturing
m_deviceStateChanged->SetState( DeviceState::DeviceStateDiscontinuity, S_OK, true );
m_deviceStateChanged->SetState( DeviceState::DeviceStateCapturing, S_OK, false );
}
// Zero out sample if silence
if ( (captureFlags & AUDCLNT_BUFFERFLAGS_SILENT) || isSilence )
{
memset( data, 0, framesAvailable * m_mixFormat->nBlockAlign );
}
//Update circular buffer
m_buffer->SetCaptureBuffer(bytesToCapture, data);
// Release buffer back
m_audioCaptureClient->ReleaseBuffer(framesAvailable);
}
exit:
return hr;
}