This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathWASAPIManager.cpp
402 lines (336 loc) · 13.8 KB
/
WASAPIManager.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
//--------------------------------------------------------------------------------------
// WASAPIManager.cpp
//
// Wraps the underlying WASAPIRenderer in a simple WinRT class that can receive
// the DeviceStateChanged events.
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "WASAPIManager.h"
using namespace Platform;
using namespace Windows::Media::Devices;
using namespace Windows::Foundation;
WASAPIManager::WASAPIManager() :
m_RenderStateChangedEvent( nullptr ),
m_CaptureStateChangedEvent( nullptr ),
m_Renderer( nullptr ),
m_CaptureIndex ( 0 ),
m_bUseLoopback ( false )
{
ZeroMemory( &m_CaptureWfx, sizeof( WAVEFORMATEX ) );
ZeroMemory( &m_RenderWfx, sizeof( WAVEFORMATEX ) );
//Create a 32k buffer
m_captureBuffer = new CBuffer(32768);
m_renderEventToken = MediaDevice::DefaultAudioRenderDeviceChanged += ref new TypedEventHandler<Platform::Object^, DefaultAudioRenderDeviceChangedEventArgs^>(this, &WASAPIManager::OnRenderDeviceChange);
InitializeCriticalSectionEx(&m_CritSec, 0, 0);
}
WASAPIManager::~WASAPIManager()
{
if (m_RenderDeviceStateChangeToken.Value != 0)
{
m_RenderStateChangedEvent->StateChangedEvent -= m_RenderDeviceStateChangeToken;
m_RenderStateChangedEvent = nullptr;
m_RenderDeviceStateChangeToken.Value = 0;
}
if (m_CaptureDeviceStateChangeToken.Value != 0)
{
m_CaptureStateChangedEvent->StateChangedEvent -= m_CaptureDeviceStateChangeToken;
m_CaptureStateChangedEvent = nullptr;
m_CaptureDeviceStateChangeToken.Value = 0;
}
ZeroMemory( &m_CaptureWfx, sizeof( WAVEFORMATEX ) );
ZeroMemory( &m_RenderWfx, sizeof( WAVEFORMATEX ) );
DeleteCriticalSection( &m_CritSec );
}
void WASAPIManager::OnRenderDeviceChange(Platform::Object^,
Windows::Media::Devices::DefaultAudioRenderDeviceChangedEventArgs^)
{
RestartDevice();
}
//--------------------------------------------------------------------------------------
// Name: OnDeviceStateChange
// Desc: Event callback from WASAPI components for changes in device state
//--------------------------------------------------------------------------------------
void WASAPIManager::OnDeviceStateChange( Object^ sender, DeviceStateChangedEventArgs^ e )
{
// Handle state specific messages
switch( e->State )
{
case DeviceState::DeviceStateInitialized:
InitializeCaptureDevice();
break;
case DeviceState::DeviceStateInError:
if (m_RenderDeviceStateChangeToken.Value != 0)
{
m_RenderStateChangedEvent->StateChangedEvent -= m_RenderDeviceStateChangeToken;
m_RenderStateChangedEvent = nullptr;
m_RenderDeviceStateChangeToken.Value = 0;
}
if (m_CaptureDeviceStateChangeToken.Value != 0)
{
m_CaptureStateChangedEvent->StateChangedEvent -= m_CaptureDeviceStateChangeToken;
m_CaptureStateChangedEvent = nullptr;
m_CaptureDeviceStateChangeToken.Value = 0;
}
m_Renderer = nullptr;
}
UpdateStatus();
}
//--------------------------------------------------------------------------------------
// Name: InitializeRenderDevice
// Desc: Sets up a new instance of the WASAPI renderer and creates WASAPI session
//--------------------------------------------------------------------------------------
void WASAPIManager::InitializeRenderDevice()
{
if (!m_Renderer)
{
// Create a new WASAPI instance
m_Renderer = Microsoft::WRL::Make<WASAPIRenderer>();
if (nullptr == m_Renderer)
{
OnDeviceStateChange( this, ref new DeviceStateChangedEventArgs( DeviceState::DeviceStateInError, E_OUTOFMEMORY ));
return;
}
// Get a pointer to the device event interface
m_RenderStateChangedEvent = m_Renderer->GetDeviceStateEvent();
if (nullptr == m_RenderStateChangedEvent)
{
OnDeviceStateChange( this, ref new DeviceStateChangedEventArgs( DeviceState::DeviceStateInError, E_FAIL ));
return;
}
// Register for events
m_RenderDeviceStateChangeToken = m_RenderStateChangedEvent->StateChangedEvent += ref new DeviceStateChangedHandler( this, &WASAPIManager::OnDeviceStateChange );
// Selects the Default Audio Device
m_Renderer->InitializeAudioDeviceAsync();
}
}
//--------------------------------------------------------------------------------------
// Name: InitializeCaptureDevice
// Desc: Sets up a new instance of the WASAPI capture
//--------------------------------------------------------------------------------------
void WASAPIManager::InitializeCaptureDevice()
{
if (!m_Renderer)
{
InitializeRenderDevice();
}
if (!m_Capture)
{
m_Capture = Microsoft::WRL::Make<WASAPICapture>();
if (nullptr == m_Capture)
{
OnDeviceStateChange( this, ref new DeviceStateChangedEventArgs( DeviceState::DeviceStateInError, E_OUTOFMEMORY ));
return;
}
// Get a pointer to the device event interface
m_CaptureStateChangedEvent = m_Capture->GetDeviceStateEvent();
if (nullptr == m_CaptureStateChangedEvent)
{
OnDeviceStateChange( this, ref new DeviceStateChangedEventArgs( DeviceState::DeviceStateInError, E_FAIL ));
return;
}
// Register for events
m_CaptureDeviceStateChangeToken = m_CaptureStateChangedEvent->StateChangedEvent += ref new DeviceStateChangedHandler( this, &WASAPIManager::OnDeviceStateChange );
//Activate the capture device
if (m_Capture->Activate(m_CaptureIndex, m_bUseLoopback, m_captureBuffer) != S_OK)
{
m_Capture = nullptr;
return;
}
//Get a copy of the capture/render format
CopyMemory(&m_CaptureWfx, m_Capture->GetFormat(), sizeof(WAVEFORMATEX));
CopyMemory(&m_RenderWfx, m_Renderer->GetFormat(), sizeof(WAVEFORMATEX));
//Pass the capture buffer to the renderer (for loopback)
m_Renderer->SetCaptureBuffer(m_captureBuffer);
//Provide the filename to capture
m_Capture->SetCaptureFilename(g_FileName);
//Give the capture buffer format information
m_captureBuffer->SetRenderFormat(&m_RenderWfx);
m_captureBuffer->SetSourceFormat(&m_CaptureWfx);
//Register the device manager
Microsoft::WRL::ComPtr<IMMDeviceEnumerator> deviceEnum;
CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnum);
deviceEnum->RegisterEndpointNotificationCallback(&m_deviceManager);
}
UpdateStatus();
}
//--------------------------------------------------------------------------------------
// Name: RecordToggle
// Desc: Toggles the state of recording
//--------------------------------------------------------------------------------------
void WASAPIManager::RecordToggle()
{
if (m_Capture)
{
DeviceState captureDeviceState = m_CaptureStateChangedEvent->GetState();
DeviceState renderDeviceState = m_RenderStateChangedEvent->GetState();
if (captureDeviceState == DeviceState::DeviceStateCapturing)
{
// Starts a work item to stop capture
m_Capture->StopCaptureAsync();
if (renderDeviceState == DeviceState::DeviceStatePlaying)
{
// Starts a work item to stop playback
m_Renderer->StopPlaybackAsync();
}
}
else if (captureDeviceState == DeviceState::DeviceStateStopped ||
captureDeviceState == DeviceState::DeviceStateInitialized)
{
if (renderDeviceState == DeviceState::DeviceStatePlaying)
{
//Renderer is playing and needs to stop
m_Renderer->StopPlaybackAsync();
}
// Starts a work item to start capture
m_Capture->StartCaptureAsync();
if(m_bUseLoopback)
{
if (renderDeviceState == DeviceState::DeviceStatePaused ||
renderDeviceState == DeviceState::DeviceStateStopped ||
renderDeviceState == DeviceState::DeviceStateInitialized)
{
// Starts a work item to start playback for loopback
m_Renderer->StartPlaybackAsync(true);
}
}
}
}
}
//--------------------------------------------------------------------------------------
// Name: LoopbackToggle
// Desc: Toggles loopback (playback of the capture device)
//--------------------------------------------------------------------------------------
void WASAPIManager::LoopbackToggle()
{
if(nullptr != m_Capture)
{
if(!m_bUseLoopback)
{
if(m_CaptureStateChangedEvent->GetState() == DeviceState::DeviceStateCapturing &&
(m_RenderStateChangedEvent->GetState() == DeviceState::DeviceStatePaused ||
m_RenderStateChangedEvent->GetState() == DeviceState::DeviceStateInitialized ||
m_RenderStateChangedEvent->GetState() == DeviceState::DeviceStateStopped))
{
//Already capturing and want loopback, so start the renderer
m_Renderer->StartPlaybackAsync(true);
}
}
else
{
if(m_CaptureStateChangedEvent->GetState() == DeviceState::DeviceStateCapturing &&
m_RenderStateChangedEvent->GetState() == DeviceState::DeviceStatePlaying)
{
//Already capturing and don't want loopback, so stop the renderer
m_Renderer->StopPlaybackAsync();
}
}
//Update the status in the capture and render components
m_Capture->LoopbackToggle();
m_bUseLoopback = !m_bUseLoopback;
UpdateStatus();
}
}
//--------------------------------------------------------------------------------------
// Name: SetCaptureDevice
// Desc: Switches the capture device
//--------------------------------------------------------------------------------------
void WASAPIManager::SetCaptureDevice(int index)
{
if(m_Capture)
{
DeviceState captureDeviceState = m_CaptureStateChangedEvent->GetState();
DeviceState renderDeviceState = m_RenderStateChangedEvent->GetState();
if (captureDeviceState == DeviceState::DeviceStateCapturing)
{
// Starts a work item to stop capture
m_Capture->StopCaptureAsync();
if (renderDeviceState == DeviceState::DeviceStatePlaying)
{
// Starts a work item to stop playback
m_Renderer->StopPlaybackAsync();
}
}
m_deviceManager.SetCaptureId(index);
m_CaptureIndex = index;
m_Capture->SetCaptureDevice(index);
m_Capture = nullptr;
InitializeCaptureDevice();
}
}
//--------------------------------------------------------------------------------------
// Name: StartDevice
// Desc: Initialize devices
//--------------------------------------------------------------------------------------
void WASAPIManager::StartDevice()
{
if (nullptr == m_Renderer)
{
InitializeRenderDevice();
}
}
//--------------------------------------------------------------------------------------
// Name: RestartDevice
// Desc: Restart playback
//--------------------------------------------------------------------------------------
void WASAPIManager::RestartDevice()
{
m_Renderer = nullptr;
InitializeRenderDevice();
}
//--------------------------------------------------------------------------------------
// Name: GetStatus
// Desc: Gets the status
//--------------------------------------------------------------------------------------
void WASAPIManager::GetStatus(ManagerStatus *inStatus)
{
EnterCriticalSection( &m_CritSec );
CopyMemory(inStatus, &m_Status, sizeof(ManagerStatus));
LeaveCriticalSection( &m_CritSec );
}
//--------------------------------------------------------------------------------------
// Name: UpdateStatus
// Desc: Updates manager's status
//--------------------------------------------------------------------------------------
void WASAPIManager::UpdateStatus()
{
EnterCriticalSection( &m_CritSec );
if(m_Capture && m_Renderer)
{
DeviceState captureDeviceState = m_CaptureStateChangedEvent->GetState();
DeviceState renderDeviceState = m_RenderStateChangedEvent->GetState();
m_Status.bCapturing = (captureDeviceState == DeviceState::DeviceStateCapturing);
m_Status.bLoopback = m_bUseLoopback;
m_Status.bPlaying = (renderDeviceState == DeviceState::DeviceStatePlaying);
}
LeaveCriticalSection( &m_CritSec );
}
//--------------------------------------------------------------------------------------
// Name: PlayPauseToggle
// Desc: Toggle pause state
//--------------------------------------------------------------------------------------
void WASAPIManager::PlayPauseToggle()
{
if (m_Renderer)
{
if(m_CaptureStateChangedEvent->GetState() == DeviceState::DeviceStateCapturing)
{
//Stop recording
RecordToggle();
}
else if(m_RenderStateChangedEvent->GetState() == DeviceState::DeviceStatePlaying)
{
//Starts a work item to pause playback
m_Renderer->PausePlaybackAsync();
}
else
{
//Tell the renderer to open the file and prepare the buffers
m_Renderer->ConfigureSource(g_FileName);
//Starts a work item to start playback
m_Renderer->StartPlaybackAsync(false);
}
}
}