forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WASAPIManager.cpp
224 lines (189 loc) · 6.78 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
//--------------------------------------------------------------------------------------
// WASAPIManager.cpp
//
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "WASAPIManager.h"
using namespace Platform;
WASAPIManager::WASAPIManager() :
m_StateChangedEvent( nullptr ),
m_Renderer( nullptr )
{
}
WASAPIManager::~WASAPIManager()
{
if (m_DeviceStateChangeToken.Value != 0)
{
m_StateChangedEvent->StateChangedEvent -= m_DeviceStateChangeToken;
m_StateChangedEvent = nullptr;
m_DeviceStateChangeToken.Value = 0;
}
}
//--------------------------------------------------------------------------------------
// Name: OnDeviceStateChange
// Desc: Event callback from WASAPI renderer for changes in device state
//--------------------------------------------------------------------------------------
void WASAPIManager::OnDeviceStateChange( Object^ sender, DeviceStateChangedEventArgs^ e )
{
// Handle state specific messages
switch( e->State )
{
case DeviceState::DeviceStateInitialized:
StartDevice();
break;
case DeviceState::DeviceStatePlaying:
OutputDebugString( L"Playback Started\n" );
break;
case DeviceState::DeviceStatePaused:
OutputDebugString( L"Playback Paused\n" );
break;
case DeviceState::DeviceStateStopped:
m_Renderer = nullptr;
if (m_DeviceStateChangeToken.Value != 0)
{
m_StateChangedEvent->StateChangedEvent -= m_DeviceStateChangeToken;
m_StateChangedEvent = nullptr;
m_DeviceStateChangeToken.Value = 0;
}
OutputDebugString( L"Playback Stopped\n" );
break;
case DeviceState::DeviceStateInError:
HRESULT hr = e->hr;
if (m_DeviceStateChangeToken.Value != 0)
{
m_StateChangedEvent->StateChangedEvent -= m_DeviceStateChangeToken;
m_StateChangedEvent = nullptr;
m_DeviceStateChangeToken.Value = 0;
}
m_Renderer = nullptr;
wchar_t hrVal[11];
swprintf_s( hrVal, _countof( hrVal ), L"0x%08x", hr );
String^ strHRVal = ref new String( hrVal );
String^ strMessage = "";
// Specifically handle a couple of known errors
switch( hr )
{
case AUDCLNT_E_ENDPOINT_OFFLOAD_NOT_CAPABLE:
strMessage = "ERROR: Endpoint Does Not Support HW Offload (" + strHRVal + ")\n";
OutputDebugString(strMessage->Data());
break;
case AUDCLNT_E_RESOURCES_INVALIDATED:
strMessage = "ERROR: Endpoint Lost Access To Resources (" + strHRVal + ")\n";
OutputDebugString( strMessage->Data() );
break;
default:
strMessage = "ERROR: " + strHRVal + " has occurred.\n";
OutputDebugString( strMessage->Data() );
}
}
}
//--------------------------------------------------------------------------------------
// Name: InitializeDevice
// Desc: Sets up a new instance of the WASAPI renderer
//--------------------------------------------------------------------------------------
void WASAPIManager::InitializeDevice()
{
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_StateChangedEvent = m_Renderer->GetDeviceStateEvent();
if (nullptr == m_StateChangedEvent)
{
OnDeviceStateChange( this, ref new DeviceStateChangedEventArgs( DeviceState::DeviceStateInError, E_FAIL ));
return;
}
// Register for events
m_DeviceStateChangeToken = m_StateChangedEvent->StateChangedEvent += ref new DeviceStateChangedHandler( this, &WASAPIManager::OnDeviceStateChange );
// Configure user based properties
DEVICEPROPS props = { 0 };
int BufferSize = 0;
props.IsBackground = false;
props.hnsBufferDuration = static_cast<REFERENCE_TIME>(BufferSize);
props.Frequency = 440;
m_Renderer->SetProperties( props );
// Selects the Default Audio Device
m_Renderer->InitializeAudioDeviceAsync();
}
}
//--------------------------------------------------------------------------------------
// Name: StartDevice
// Desc: Initialize and start playback
//--------------------------------------------------------------------------------------
void WASAPIManager::StartDevice()
{
if (nullptr == m_Renderer)
{
// Call from main UI thread
InitializeDevice();
}
else
{
// Starts a work item to begin playback, likely in the paused state
m_Renderer->StartPlaybackAsync();
}
}
//--------------------------------------------------------------------------------------
// Name: StopDevice
// Desc: Stop playback, if WASAPI renderer exists
//--------------------------------------------------------------------------------------
void WASAPIManager::StopDevice()
{
if (m_Renderer)
{
// Set the event to stop playback
m_Renderer->StopPlaybackAsync();
}
}
//--------------------------------------------------------------------------------------
// Name: PauseDevice
// Desc: If device is playing, pause playback. Otherwise do nothing.
//--------------------------------------------------------------------------------------
void WASAPIManager::PauseDevice()
{
if (m_Renderer)
{
DeviceState deviceState = m_StateChangedEvent->GetState();
if (deviceState == DeviceState::DeviceStatePlaying)
{
// Starts a work item to pause playback
m_Renderer->PausePlaybackAsync();
}
}
}
//--------------------------------------------------------------------------------------
// Name: PlayPauseToggle
// Desc: Toggle pause state
//--------------------------------------------------------------------------------------
void WASAPIManager::PlayPauseToggle()
{
if (m_Renderer)
{
DeviceState deviceState = m_StateChangedEvent->GetState();
// We only permit a pause state change if we're fully playing
// or fully paused.
if (deviceState == DeviceState::DeviceStatePlaying)
{
// Starts a work item to pause playback
m_Renderer->PausePlaybackAsync();
}
else if (deviceState == DeviceState::DeviceStatePaused)
{
// Starts a work item to pause playback
m_Renderer->StartPlaybackAsync();
}
}
else
{
StartDevice();
}
}