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
|
//
// FormatHelpers.h -- Copyright (c) Microsoft Corporation
//
// Author: zaneh
//
// Description:
//
// Format Helpers for test formats used in APODeviceTest
//
#pragma once
#include <MMDeviceAPIp.h>
#include <DeviceTopologyp.h>
#include <mfapi.h>
#include <AVEndpointKeys.h>
#include <AudioCoreAPO32Params.h>
#include <ksmedia.h>
// Consts used for APO format creation
static const int cFrameRate = 48000; // 48kHz
static const int cProcessingPeriod = 10; // milliseconds
static const int cMaxFrameCount = cFrameRate * cProcessingPeriod / 1000;
static const int cFrameCountToProcess = cMaxFrameCount / 2;
// Returns true if the two IAudioMediaType formats are equal.
// False otherwise
bool IsEqualFormat
(
IAudioMediaType *pIAMTFormat1,
IAudioMediaType *pIAMTFormat2
)
{
UINT cbBuffer1, cbBuffer2;
WAVEFORMATEX const *pwfx1, *pwfx2;
UNCOMPRESSEDAUDIOFORMAT format1, format2;
pwfx1 = pIAMTFormat1->GetAudioFormat();
pwfx2 = pIAMTFormat2->GetAudioFormat();
cbBuffer1 = ((WAVE_FORMAT_PCM == pwfx1->wFormatTag)?sizeof(PCMWAVEFORMAT):(sizeof(WAVEFORMATEX) + pwfx1->cbSize));
cbBuffer2 = ((WAVE_FORMAT_PCM == pwfx2->wFormatTag)?sizeof(PCMWAVEFORMAT):(sizeof(WAVEFORMATEX) + pwfx2->cbSize));
if (cbBuffer1 != cbBuffer2)
{
return false;
}
// Two formats don't compare as WAVEFORMATEX...
if (0 != memcmp(pwfx1, pwfx2, cbBuffer1))
{
return false;
}
SecureZeroMemory(&format1, sizeof(format1));
SecureZeroMemory(&format2, sizeof(format2));
if (S_OK != pIAMTFormat1->GetUncompressedAudioFormat(&format1))
{
return false;
}
if (S_OK != pIAMTFormat2->GetUncompressedAudioFormat(&format2))
{
return false;
}
// Two formats don't compare as WAVEFORMATEX...
if (0 != memcmp(&format1, &format2, sizeof(UNCOMPRESSEDAUDIOFORMAT)))
{
return false;
}
return true;
}
// Creates a KSData format from the given WaveFormatEx
// Returns a struct containing both the KSData format and WaveFormatEx
KSDATAFORMAT_WAVEFORMATEX *CreateKSDataFromWFX
(
WAVEFORMATEX *pwfx
)
{
if (nullptr == pwfx)
{
return nullptr;
}
UINT cbAlloc;
cbAlloc = sizeof(KSDATAFORMAT_WAVEFORMATEX) + pwfx->cbSize;
wil::unique_cotaskmem_ptr<KSDATAFORMAT_WAVEFORMATEX> pKsFormat((KSDATAFORMAT_WAVEFORMATEX*)(CoTaskMemAlloc(cbAlloc)));
if (nullptr == pKsFormat)
{
return nullptr;
}
pKsFormat->DataFormat.FormatSize = cbAlloc;
pKsFormat->DataFormat.Flags = 0;
pKsFormat->DataFormat.Reserved = 0;
pKsFormat->DataFormat.MajorFormat = KSDATAFORMAT_TYPE_AUDIO;
pKsFormat->DataFormat.Specifier = KSDATAFORMAT_SPECIFIER_WAVEFORMATEX;
CopyMemory(&(pKsFormat->WaveFormatEx), pwfx, sizeof(WAVEFORMATEX) + pwfx->cbSize);
if (WAVE_FORMAT_EXTENSIBLE == pwfx->wFormatTag)
{
pKsFormat->DataFormat.SubFormat = ((WAVEFORMATEXTENSIBLE*)(pwfx))->SubFormat;
}
else
{
INIT_WAVEFORMATEX_GUID(&(pKsFormat->DataFormat.SubFormat), pwfx->wFormatTag);
}
return (pKsFormat.release());
}
// Creates an IAudioMediaType given an uncompressed audio format
HRESULT AECreateAudioMediaTypeFromUncompressedAudioFormat(const UNCOMPRESSEDAUDIOFORMAT* pUncompressedAudioFormat,IAudioMediaType** ppIAudioMediaType)
{
RETURN_IF_FAILED(CTestAudioMediaType::Create(pUncompressedAudioFormat, ppIAudioMediaType));
return S_OK;
}
// Creates an IAudioMediaType given a WaveFormatEx
HRESULT AECreateAudioMediaType(const WAVEFORMATEX* pWfx, IAudioMediaType** ppIAudioMediaType)
{
UNCOMPRESSEDAUDIOFORMAT audioFormat;
audioFormat.dwBytesPerSampleContainer = pWfx->nBlockAlign / pWfx->nChannels;
audioFormat.dwSamplesPerFrame = pWfx->nChannels;
if (WAVE_FORMAT_EXTENSIBLE == pWfx->wFormatTag)
{
audioFormat.dwValidBitsPerSample = ((WAVEFORMATEXTENSIBLE*)pWfx)->Samples.wValidBitsPerSample;
audioFormat.fFramesPerSecond = (float)(((WAVEFORMATEXTENSIBLE*)pWfx)->Format.nSamplesPerSec);
audioFormat.guidFormatType = ((WAVEFORMATEXTENSIBLE*)pWfx)->SubFormat;
}
else
{
audioFormat.dwValidBitsPerSample = pWfx->wBitsPerSample;
audioFormat.fFramesPerSecond = (float)(pWfx->nSamplesPerSec);
INIT_WAVEFORMATEX_GUID(&(audioFormat.guidFormatType), pWfx->wFormatTag);
}
RETURN_IF_FAILED(CTestAudioMediaType::Create(&audioFormat, ppIAudioMediaType));
return S_OK;
}
// Fills an APO connection using the provided uncompressed format.
// If pBuffer is NULL, the connection will be created with an APO_CONNECTION_BUFFER_TYPE_ALLOCATED.
// If pBuffer is not null, APO_CONNECTION_BUFFER_TYPE_EXTERNAL will be used.
// Outptr pConnection returns an APO_CONNECTION_DESCRIPTOR
HRESULT FillConnection
(
UNCOMPRESSEDAUDIOFORMAT* Format,
UINT_PTR pBuffer,
UINT32 /* u32ExtraFrameCount */,
UINT32 u32MaxFrameCount,
APO_CONNECTION_DESCRIPTOR* pConnection
)
{
if (Format == nullptr)
{
LOG_ERROR(L"FillConnection recieved null format");
return E_INVALIDARG;
}
if (pConnection == nullptr)
{
LOG_ERROR(L"FillConnection recieved null APO connection descriptor");
return E_INVALIDARG;
}
IAudioMediaType *pFormat;
// Attempt to create the AudioMediaType using the standard API
HRESULT createFormatHr = CreateAudioMediaTypeFromUncompressedAudioFormat(
Format,
&pFormat);
if (FAILED(createFormatHr))
{
LOG_OUTPUT(L"Standard AudioMediaType creation failed. Attempting creation using TestMediaType");
RETURN_IF_FAILED(AECreateAudioMediaTypeFromUncompressedAudioFormat(Format, &pFormat));
}
// set connection type based on pBuffer
// this will get re(set) when the actual allocation occurs
if (NULL != pBuffer)
{
pConnection->Type = APO_CONNECTION_BUFFER_TYPE_EXTERNAL;
}
else
{
pConnection->Type = APO_CONNECTION_BUFFER_TYPE_ALLOCATED;
}
pConnection->pFormat = pFormat;
pConnection->pBuffer = pBuffer;
pConnection->u32MaxFrameCount = u32MaxFrameCount;
pConnection->u32Signature = APO_CONNECTION_DESCRIPTOR_SIGNATURE;
return createFormatHr;
}
// Creates a WaveFormatEx format
void FillFormat
(
WORD FormatType,
DWORD dwSamplesPerFrame,
DWORD /* dwBytesPerSampleContainer */,
DWORD dwValidBitsPerSample,
FLOAT32 fFramesPerSecond,
WAVEFORMATEX* pWfx
)
{
if (pWfx == nullptr)
{
LOG_ERROR(L"WaveformatEx is null");
}
if (dwSamplesPerFrame >= 0xffff)
{
LOG_ERROR(L"SamplesPerFrame too great");
}
if (dwValidBitsPerSample >= 0xffff)
{
LOG_ERROR(L"ValidBitsPerSample too great");
}
pWfx->wFormatTag = FormatType;
pWfx->nChannels = (WORD)(dwSamplesPerFrame & 0xffff);
pWfx->wBitsPerSample = (WORD)dwValidBitsPerSample;
pWfx->nBlockAlign = pWfx->nChannels * (pWfx->wBitsPerSample / 8);
pWfx->nSamplesPerSec = (DWORD)fFramesPerSecond;
pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
pWfx->cbSize = 0;
}
// Creates an UncompressedAudioFormat
void FillFormat
(
GUID FormatType,
DWORD dwSamplesPerFrame,
DWORD dwBytesPerSampleContainer,
DWORD dwValidBitsPerSample,
FLOAT32 fFramesPerSecond,
DWORD dwChannelMask,
UNCOMPRESSEDAUDIOFORMAT* pFormat
)
{
if (pFormat == nullptr)
{
LOG_ERROR(L"WaveformatEx is null");
}
if (dwSamplesPerFrame >= 0xffff)
{
LOG_ERROR(L"SamplesPerFrame too great");
}
if (dwValidBitsPerSample >= 0xffff)
{
LOG_ERROR(L"ValidBitsPerSample too great");
}
pFormat->guidFormatType = FormatType;
pFormat->dwSamplesPerFrame = dwSamplesPerFrame;
pFormat->dwBytesPerSampleContainer = dwBytesPerSampleContainer;
pFormat->dwValidBitsPerSample = dwValidBitsPerSample;
pFormat->fFramesPerSecond = fFramesPerSecond;
pFormat->dwChannelMask = dwChannelMask;
}
HRESULT SetupConnection
(
APO_CONNECTION_DESCRIPTOR* pInputConnDesc,
APO_CONNECTION_DESCRIPTOR* pOutputConnDesc,
APO_CONNECTION_PROPERTY* pInputConnProp,
APO_CONNECTION_PROPERTY* pOutputConnProp,
UNCOMPRESSEDAUDIOFORMAT* pDefaultFormat
)
{
if (pInputConnDesc == nullptr)
{
LOG_ERROR(L"SetupConnection recieved null APO connection descriptor for input");
return E_INVALIDARG;
}
if (pOutputConnDesc == nullptr)
{
LOG_ERROR(L"SetupConnection recieved null APO connection descriptor for output");
return E_INVALIDARG;
}
if (pInputConnProp == nullptr)
{
LOG_ERROR(L"SetupConnection recieved null APO connection property for input");
return E_INVALIDARG;
}
if (pOutputConnProp == nullptr)
{
LOG_ERROR(L"SetupConnection recieved null APO connection property for output");
return E_INVALIDARG;
}
if (pDefaultFormat == nullptr)
{
LOG_ERROR(L"SetupConnection received null format");
return E_INVALIDARG;
}
LOG_RETURN_IF_FAILED(FillConnection(pDefaultFormat, NULL, 0, cMaxFrameCount, pInputConnDesc),
L"\tFillConnection for input descriptor failed");
LOG_RETURN_IF_FAILED(CreateConnection(pInputConnDesc, pInputConnProp),
L"\tCreateConnection for input descriptor failed");
pInputConnProp->u32ValidFrameCount = cFrameCountToProcess;
pInputConnProp->u32BufferFlags = BUFFER_VALID;
LOG_RETURN_IF_FAILED(FillConnection(pDefaultFormat, NULL, 0, cMaxFrameCount, pOutputConnDesc),
L"\tFillConnection for output descriptor failed");
LOG_RETURN_IF_FAILED(CreateConnection(pOutputConnDesc, pOutputConnProp),
L"\tCreateConnection for output descriptor failed");
return S_OK;
}
|