mmdevapi: be stricter about tests
[wine/multimedia.git] / dlls / mmdevapi / tests / render.c
blob665eaa5019942517f49dcf56b4888ce4b9eba40f
1 /*
2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
3 * 2011-2012 Jörg Höhle
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* This test is for audio playback specific mechanisms
21 * Tests:
22 * - IAudioClient with eRender and IAudioRenderClient
25 #include <math.h>
26 #include <stdio.h>
28 #include "wine/test.h"
30 #define COBJMACROS
32 #ifdef STANDALONE
33 #include "initguid.h"
34 #endif
36 #include "unknwn.h"
37 #include "uuids.h"
38 #include "mmdeviceapi.h"
39 #include "audioclient.h"
40 #include "audiopolicy.h"
42 static const unsigned int win_formats[][4] = {
43 { 8000, 8, 1}, { 8000, 8, 2}, { 8000, 16, 1}, { 8000, 16, 2},
44 {11025, 8, 1}, {11025, 8, 2}, {11025, 16, 1}, {11025, 16, 2},
45 {12000, 8, 1}, {12000, 8, 2}, {12000, 16, 1}, {12000, 16, 2},
46 {16000, 8, 1}, {16000, 8, 2}, {16000, 16, 1}, {16000, 16, 2},
47 {22050, 8, 1}, {22050, 8, 2}, {22050, 16, 1}, {22050, 16, 2},
48 {44100, 8, 1}, {44100, 8, 2}, {44100, 16, 1}, {44100, 16, 2},
49 {48000, 8, 1}, {48000, 8, 2}, {48000, 16, 1}, {48000, 16, 2},
50 {96000, 8, 1}, {96000, 8, 2}, {96000, 16, 1}, {96000, 16, 2}
52 #define NB_WIN_FORMATS (sizeof(win_formats)/sizeof(*win_formats))
54 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
56 static IMMDeviceEnumerator *mme = NULL;
57 static IMMDevice *dev = NULL;
58 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
60 static inline const char *dbgstr_guid( const GUID *id )
62 static char ret[256];
63 sprintf(ret, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
64 id->Data1, id->Data2, id->Data3,
65 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
66 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
67 return ret;
70 #define PI 3.14159265358979323846L
71 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
73 static double phase = 0.; /* normalized to unit, not 2*PI */
74 PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
75 DWORD cn, i;
76 double delta, y;
78 if(!winetest_interactive || wfxe->Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE ||
79 !IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
80 return AUDCLNT_BUFFERFLAGS_SILENT;
82 for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
83 delta += .5/wfxe->Format.nChannels, cn++){
84 for(i = 0; i < frames; i++){
85 y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
86 /* assume alignment is granted */
87 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
88 /* fixme: 16bit for exclusive mode */
91 phase += 440.* frames / wfxe->Format.nSamplesPerSec;
92 phase -= floor(phase);
93 return 0;
96 static void test_uninitialized(IAudioClient *ac)
98 HRESULT hr;
99 UINT32 num;
100 REFERENCE_TIME t1;
102 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
103 IUnknown *unk;
105 hr = IAudioClient_GetBufferSize(ac, &num);
106 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
108 hr = IAudioClient_GetStreamLatency(ac, &t1);
109 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
111 hr = IAudioClient_GetCurrentPadding(ac, &num);
112 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
114 hr = IAudioClient_Start(ac);
115 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
117 hr = IAudioClient_Stop(ac);
118 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
120 hr = IAudioClient_Reset(ac);
121 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
123 hr = IAudioClient_SetEventHandle(ac, handle);
124 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
126 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
127 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
129 CloseHandle(handle);
132 static void test_audioclient(void)
134 IAudioClient *ac;
135 IUnknown *unk;
136 HRESULT hr;
137 ULONG ref;
138 WAVEFORMATEX *pwfx, *pwfx2;
139 REFERENCE_TIME t1, t2;
140 HANDLE handle;
142 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
143 NULL, (void**)&ac);
144 ok(hr == S_OK, "Activation failed with %08x\n", hr);
145 if(hr != S_OK)
146 return;
148 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
150 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
151 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
153 unk = (void*)(LONG_PTR)0x12345678;
154 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
155 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
156 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
158 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
159 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
160 if (unk)
162 ref = IUnknown_Release(unk);
163 ok(ref == 1, "Released count is %u\n", ref);
166 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
167 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
168 if (unk)
170 ref = IUnknown_Release(unk);
171 ok(ref == 1, "Released count is %u\n", ref);
174 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
175 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
177 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
178 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
180 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
181 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
183 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
184 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
185 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
186 (UINT)(t1/10000), (UINT)(t1 % 10000),
187 (UINT)(t2/10000), (UINT)(t2 % 10000));
189 hr = IAudioClient_GetMixFormat(ac, NULL);
190 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
192 hr = IAudioClient_GetMixFormat(ac, &pwfx);
193 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
195 if (hr == S_OK)
197 trace("pwfx: %p\n", pwfx);
198 trace("Tag: %04x\n", pwfx->wFormatTag);
199 trace("bits: %u\n", pwfx->wBitsPerSample);
200 trace("chan: %u\n", pwfx->nChannels);
201 trace("rate: %u\n", pwfx->nSamplesPerSec);
202 trace("align: %u\n", pwfx->nBlockAlign);
203 trace("extra: %u\n", pwfx->cbSize);
204 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
205 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
207 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
208 trace("Res: %u\n", pwfxe->Samples.wReserved);
209 trace("Mask: %x\n", pwfxe->dwChannelMask);
210 trace("Alg: %s\n",
211 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
212 (IsEqualGUID(&pwfxe->SubFormat,
213 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
216 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
217 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
218 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
219 CoTaskMemFree(pwfx2);
221 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
222 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
224 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
225 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
227 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
228 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
229 "IsFormatSupported(Exclusive) call returns %08x\n", hr);
230 hexcl = hr;
232 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
233 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
234 ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
235 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
237 if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
238 hexcl = S_OK;
240 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
241 ok(hr == E_INVALIDARG/*w32*/ ||
242 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
243 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
246 test_uninitialized(ac);
248 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
249 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
251 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
252 ok(hr == E_INVALIDARG, "Initialize with invalid flags returns %08x\n", hr);
254 /* A period != 0 is ignored and the call succeeds.
255 * Since we can only initialize successfully once, skip those tests.
257 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
258 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
260 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
261 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
262 if(hr == S_OK){
263 UINT32 num;
265 hr = IAudioClient_GetBufferSize(ac, &num);
266 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
267 if(hr == S_OK)
268 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
271 IAudioClient_Release(ac);
273 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
274 NULL, (void**)&ac);
275 ok(hr == S_OK, "Activation failed with %08x\n", hr);
277 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
278 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
279 WAVEFORMATEX *fmt2 = NULL;
281 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
283 fmtex->dwChannelMask = 0xffff;
285 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
286 ok(hr == S_OK, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
288 IAudioClient_Release(ac);
290 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
291 NULL, (void**)&ac);
292 ok(hr == S_OK, "Activation failed with %08x\n", hr);
294 fmtex->dwChannelMask = 0;
296 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
297 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
298 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
299 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
301 CoTaskMemFree(fmt2);
303 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
304 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
306 IAudioClient_Release(ac);
308 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
309 NULL, (void**)&ac);
310 ok(hr == S_OK, "Activation failed with %08x\n", hr);
312 CoTaskMemFree(pwfx);
314 hr = IAudioClient_GetMixFormat(ac, &pwfx);
315 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
316 }else
317 skip("Skipping dwChannelMask tests\n");
319 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
320 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
321 if (hr != S_OK)
323 IAudioClient_Release(ac);
324 CoTaskMemFree(pwfx);
325 return;
328 hr = IAudioClient_GetStreamLatency(ac, NULL);
329 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
331 hr = IAudioClient_GetStreamLatency(ac, &t2);
332 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
333 trace("Returned latency: %u.%04u ms\n",
334 (UINT)(t2/10000), (UINT)(t2 % 10000));
335 ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000),
336 "Latency < default period, delta %ldus\n", (long)((t2-t1)/10));
337 /* Native appears to add the engine period to the HW latency in shared mode */
339 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
340 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
342 hr = IAudioClient_SetEventHandle(ac, NULL);
343 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
345 hr = IAudioClient_SetEventHandle(ac, handle);
346 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
347 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
348 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
349 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
350 , "SetEventHandle returns %08x\n", hr);
352 hr = IAudioClient_Reset(ac);
353 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
355 hr = IAudioClient_Reset(ac);
356 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
358 hr = IAudioClient_Stop(ac);
359 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
361 hr = IAudioClient_Start(ac);
362 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
364 hr = IAudioClient_Start(ac);
365 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
367 IAudioClient_Release(ac);
369 CloseHandle(handle);
370 CoTaskMemFree(pwfx);
373 static void test_formats(AUDCLNT_SHAREMODE mode)
375 IAudioClient *ac;
376 HRESULT hr, hrs;
377 WAVEFORMATEX fmt, *pwfx, *pwfx2;
378 int i;
380 fmt.wFormatTag = WAVE_FORMAT_PCM;
381 fmt.cbSize = 0;
383 for(i = 0; i < NB_WIN_FORMATS; i++) {
384 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
385 NULL, (void**)&ac);
386 ok(hr == S_OK, "Activation failed with %08x\n", hr);
387 if(hr != S_OK)
388 continue;
390 hr = IAudioClient_GetMixFormat(ac, &pwfx);
391 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
393 fmt.nSamplesPerSec = win_formats[i][0];
394 fmt.wBitsPerSample = win_formats[i][1];
395 fmt.nChannels = win_formats[i][2];
396 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
397 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
399 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
400 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
401 hrs = hr;
402 /* Only shared mode suggests something ... GetMixFormat! */
403 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
404 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
405 /* 5:1 card exception when asked for 1 channel at mixer rate */
406 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
407 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
408 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
409 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
410 if (hr == S_OK)
411 trace("IsSupported(%s, %ux%2ux%u)\n",
412 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
413 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
415 /* Change GetMixFormat wBitsPerSample only => S_OK */
416 if (mode == AUDCLNT_SHAREMODE_SHARED
417 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
418 && fmt.nChannels == pwfx->nChannels)
419 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
421 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
422 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
423 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
424 if (pwfx2) {
425 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
426 pwfx2->nChannels == pwfx->nChannels &&
427 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
428 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
429 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
432 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
433 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
434 if ((hrs == S_OK) ^ (hr == S_OK))
435 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
436 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
437 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
438 if (mode == AUDCLNT_SHAREMODE_SHARED)
439 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
440 "Initialize(shared, %ux%2ux%u) returns %08x\n",
441 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
442 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
443 /* Unsupported format implies "create failed" and shadows "not allowed" */
444 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
445 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
446 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
447 else
448 /* On testbot 48000x16x1 claims support, but does not Initialize.
449 * Some cards Initialize 44100|48000x16x1 yet claim no support;
450 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
451 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
452 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || broken(hr == S_OK &&
453 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
454 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
455 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
456 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
458 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
459 * Release this ac and Activate a new one.
460 * A second call (with a known working format) would yield
461 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
462 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
463 * all subsequent calls until the audio engine service is restarted. */
465 CoTaskMemFree(pwfx2);
466 CoTaskMemFree(pwfx);
467 IAudioClient_Release(ac);
471 static void test_references(void)
473 IAudioClient *ac;
474 IAudioRenderClient *rc;
475 ISimpleAudioVolume *sav;
476 IAudioStreamVolume *asv;
477 IAudioClock *acl;
478 WAVEFORMATEX *pwfx;
479 HRESULT hr;
480 ULONG ref;
482 /* IAudioRenderClient */
483 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
484 NULL, (void**)&ac);
485 ok(hr == S_OK, "Activation failed with %08x\n", hr);
486 if(hr != S_OK)
487 return;
489 hr = IAudioClient_GetMixFormat(ac, &pwfx);
490 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
492 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
493 0, pwfx, NULL);
494 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
496 CoTaskMemFree(pwfx);
498 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
499 ok(hr == S_OK, "GetService failed: %08x\n", hr);
500 if(hr != S_OK) {
501 IAudioClient_Release(ac);
502 return;
505 IAudioRenderClient_AddRef(rc);
506 ref = IAudioRenderClient_Release(rc);
507 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
509 ref = IAudioClient_Release(ac);
510 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
512 ref = IAudioRenderClient_Release(rc);
513 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
515 /* ISimpleAudioVolume */
516 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
517 NULL, (void**)&ac);
518 ok(hr == S_OK, "Activation failed with %08x\n", hr);
519 if(hr != S_OK)
520 return;
522 hr = IAudioClient_GetMixFormat(ac, &pwfx);
523 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
525 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
526 0, pwfx, NULL);
527 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
529 CoTaskMemFree(pwfx);
531 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
532 ok(hr == S_OK, "GetService failed: %08x\n", hr);
534 ISimpleAudioVolume_AddRef(sav);
535 ref = ISimpleAudioVolume_Release(sav);
536 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
538 ref = IAudioClient_Release(ac);
539 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
541 ref = ISimpleAudioVolume_Release(sav);
542 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
544 /* IAudioClock */
545 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
546 NULL, (void**)&ac);
547 ok(hr == S_OK, "Activation failed with %08x\n", hr);
548 if(hr != S_OK)
549 return;
551 hr = IAudioClient_GetMixFormat(ac, &pwfx);
552 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
554 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
555 0, pwfx, NULL);
556 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
558 CoTaskMemFree(pwfx);
560 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
561 ok(hr == S_OK, "GetService failed: %08x\n", hr);
563 IAudioClock_AddRef(acl);
564 ref = IAudioClock_Release(acl);
565 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
567 ref = IAudioClient_Release(ac);
568 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
570 ref = IAudioClock_Release(acl);
571 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
573 /* IAudioStreamVolume */
574 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
575 NULL, (void**)&ac);
576 ok(hr == S_OK, "Activation failed with %08x\n", hr);
577 if(hr != S_OK)
578 return;
580 hr = IAudioClient_GetMixFormat(ac, &pwfx);
581 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
583 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
584 0, pwfx, NULL);
585 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
587 CoTaskMemFree(pwfx);
589 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
590 ok(hr == S_OK, "GetService failed: %08x\n", hr);
592 IAudioStreamVolume_AddRef(asv);
593 ref = IAudioStreamVolume_Release(asv);
594 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
596 ref = IAudioClient_Release(ac);
597 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
599 ref = IAudioStreamVolume_Release(asv);
600 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
603 static void test_event(void)
605 HANDLE event;
606 HRESULT hr;
607 DWORD r;
608 IAudioClient *ac;
609 WAVEFORMATEX *pwfx;
611 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
612 NULL, (void**)&ac);
613 ok(hr == S_OK, "Activation failed with %08x\n", hr);
614 if(hr != S_OK)
615 return;
617 hr = IAudioClient_GetMixFormat(ac, &pwfx);
618 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
620 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
621 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
622 0, pwfx, NULL);
623 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
625 CoTaskMemFree(pwfx);
627 event = CreateEventW(NULL, FALSE, FALSE, NULL);
628 ok(event != NULL, "CreateEvent failed\n");
630 hr = IAudioClient_Start(ac);
631 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start failed: %08x\n", hr);
633 hr = IAudioClient_SetEventHandle(ac, event);
634 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
636 hr = IAudioClient_SetEventHandle(ac, event);
637 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), "SetEventHandle returns %08x\n", hr);
639 r = WaitForSingleObject(event, 40);
640 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
642 hr = IAudioClient_Start(ac);
643 ok(hr == S_OK, "Start failed: %08x\n", hr);
645 r = WaitForSingleObject(event, 20);
646 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
648 hr = IAudioClient_Stop(ac);
649 ok(hr == S_OK, "Stop failed: %08x\n", hr);
651 ok(ResetEvent(event), "ResetEvent\n");
653 /* Still receiving events! */
654 r = WaitForSingleObject(event, 20);
655 ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
657 hr = IAudioClient_Reset(ac);
658 ok(hr == S_OK, "Reset failed: %08x\n", hr);
660 ok(ResetEvent(event), "ResetEvent\n");
662 r = WaitForSingleObject(event, 120);
663 ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
665 hr = IAudioClient_SetEventHandle(ac, NULL);
666 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
668 r = WaitForSingleObject(event, 70);
669 ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
671 /* test releasing a playing stream */
672 hr = IAudioClient_Start(ac);
673 ok(hr == S_OK, "Start failed: %08x\n", hr);
674 IAudioClient_Release(ac);
676 CloseHandle(event);
679 static void test_padding(void)
681 HRESULT hr;
682 IAudioClient *ac;
683 IAudioRenderClient *arc;
684 WAVEFORMATEX *pwfx;
685 REFERENCE_TIME minp, defp;
686 BYTE *buf;
687 UINT32 psize, pad, written;
689 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
690 NULL, (void**)&ac);
691 ok(hr == S_OK, "Activation failed with %08x\n", hr);
692 if(hr != S_OK)
693 return;
695 hr = IAudioClient_GetMixFormat(ac, &pwfx);
696 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
697 if(hr != S_OK)
698 return;
700 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
701 0, 5000000, 0, pwfx, NULL);
702 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
703 if(hr != S_OK)
704 return;
706 /** GetDevicePeriod
707 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
708 * except when the HW/OS forces a particular alignment,
709 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
710 * 441 observed with Vista, 448 with w7 on the same HW! */
711 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
712 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
713 /* some wineXYZ.drv use 20ms, not seen on native */
714 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
715 "Expected 10ms default period: %u\n", (ULONG)defp);
716 ok(minp != 0, "Minimum period is 0\n");
717 ok(minp <= defp, "Mininum period is greater than default period\n");
719 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
720 ok(hr == S_OK, "GetService failed: %08x\n", hr);
722 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
724 written = 0;
725 hr = IAudioClient_GetCurrentPadding(ac, &pad);
726 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
727 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
729 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
730 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
731 ok(buf != NULL, "NULL buffer returned\n");
733 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
734 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
735 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
736 /* MSDN instead documents buf remains untouched */
738 hr = IAudioClient_Reset(ac);
739 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
741 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
742 AUDCLNT_BUFFERFLAGS_SILENT);
743 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
744 if(hr == S_OK) written += psize;
746 hr = IAudioClient_GetCurrentPadding(ac, &pad);
747 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
748 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
750 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
752 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
753 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
754 ok(buf != NULL, "NULL buffer returned\n");
756 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
757 AUDCLNT_BUFFERFLAGS_SILENT);
758 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
759 written += psize;
761 hr = IAudioClient_GetCurrentPadding(ac, &pad);
762 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
763 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
765 /* overfull buffer. requested 1/2s buffer size, so try
766 * to get a 1/2s buffer, which should fail */
767 psize = pwfx->nSamplesPerSec / 2;
768 buf = (void*)0xDEADF00D;
769 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
770 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
771 ok(buf == NULL, "NULL expected %p\n", buf);
773 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
774 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
776 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
778 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
779 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
780 ok(buf != NULL, "NULL buffer returned\n");
782 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
783 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
785 buf = (void*)0xDEADF00D;
786 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
787 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
788 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
789 /* MSDN instead documents buf remains untouched */
791 buf = (void*)0xDEADF00D;
792 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
793 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
794 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
796 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
797 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
799 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
800 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
801 ok(buf != NULL, "NULL buffer returned\n");
803 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
804 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
806 hr = IAudioClient_GetCurrentPadding(ac, &pad);
807 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
808 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
810 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
811 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
812 ok(buf != NULL, "NULL buffer returned\n");
814 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
815 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
816 /* todo_wine means Wine may overwrite memory */
817 if(hr == S_OK) written += psize+1;
819 /* Buffer still hold */
820 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
821 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
822 if(hr == S_OK) written += psize/2;
824 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
825 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
827 hr = IAudioClient_GetCurrentPadding(ac, &pad);
828 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
829 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
831 CoTaskMemFree(pwfx);
833 IAudioRenderClient_Release(arc);
834 IAudioClient_Release(ac);
837 static void test_clock(int share)
839 HRESULT hr;
840 IAudioClient *ac;
841 IAudioClock *acl;
842 IAudioRenderClient *arc;
843 UINT64 freq, pos, pcpos0, pcpos, last;
844 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
845 BYTE *data;
846 WAVEFORMATEX *pwfx;
847 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
848 REFERENCE_TIME minp, defp, t1, t2;
849 REFERENCE_TIME duration = 5000000, period = 150000;
850 int i;
852 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
854 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
855 NULL, (void**)&ac);
856 ok(hr == S_OK, "Activation failed with %08x\n", hr);
857 if(hr != S_OK)
858 return;
860 hr = IAudioClient_GetMixFormat(ac, &pwfx);
861 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
862 if(hr != S_OK)
863 return;
865 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
866 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
867 ok(minp <= period, "desired period %u to small for %u\n", (ULONG)period, (ULONG)minp);
869 if (share) {
870 trace("Testing shared mode\n");
871 /* period is ignored */
872 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
873 0, duration, period, pwfx, NULL);
874 period = defp;
875 } else {
876 pwfx->wFormatTag = WAVE_FORMAT_PCM;
877 pwfx->nChannels = 2;
878 pwfx->cbSize = 0;
879 pwfx->wBitsPerSample = 16; /* no floating point */
880 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
881 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
882 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
884 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
885 0, duration, period, pwfx, NULL);
887 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
888 if (hr != S_OK) {
889 CoTaskMemFree(pwfx);
890 IAudioClient_Release(ac);
891 if(hr == AUDCLNT_E_DEVICE_IN_USE)
892 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
893 return;
896 /** GetStreamLatency
897 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
898 * Exclusive mode: testbot returns 2x period + a little, but
899 * some HDA drivers return 1x period, some + a little. */
900 hr = IAudioClient_GetStreamLatency(ac, &t2);
901 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
902 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
903 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000),
904 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
906 /** GetBufferSize
907 * BufferSize must be rounded up, maximum 2s says MSDN.
908 * Both is wrong. Rounding may lead to size a little smaller than duration;
909 * duration > 2s is accepted in shared mode.
910 * Shared mode: round solely w.r.t. mixer rate,
911 * duration is no multiple of period.
912 * Exclusive mode: size appears as a multiple of some fragment that
913 * is either the rounded period or a fixed constant like 1024,
914 * whatever the driver implements. */
915 hr = IAudioClient_GetBufferSize(ac, &gbsize);
916 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
918 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
919 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
920 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
921 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
922 /* fragment size (= period in frames) is rounded up.
923 * BufferSize must be rounded up, maximum 2s says MSDN
924 * but it is rounded down modulo fragment ! */
925 if (share)
926 ok(gbsize == bufsize,
927 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
928 else
929 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
930 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
932 /* In shared mode, GetCurrentPadding decreases in multiples of
933 * fragment size (i.e. updated only at period ticks), whereas
934 * GetPosition appears to be reporting continuous positions.
935 * In exclusive mode, testbot behaves likewise, but native's Intel
936 * HDA driver shows no such deltas, GetCurrentPadding closely
937 * matches GetPosition, as in
938 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
940 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
941 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
943 hr = IAudioClock_GetFrequency(acl, &freq);
944 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
945 trace("Clock Frequency %u\n", (UINT)freq);
947 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
948 if (share)
949 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
950 "Clock Frequency %u\n", (UINT)freq);
951 else
952 ok(freq == pwfx->nSamplesPerSec,
953 "Clock Frequency %u\n", (UINT)freq);
955 hr = IAudioClock_GetPosition(acl, NULL, NULL);
956 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
958 pcpos0 = 0;
959 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
960 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
961 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
962 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
964 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
965 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
967 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
968 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
970 avail = gbsize;
971 data = NULL;
972 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
973 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
974 trace("data at %p\n", data);
976 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_interactive ?
977 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
978 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
979 if(hr == S_OK) sum += avail;
981 hr = IAudioClient_GetCurrentPadding(ac, &pad);
982 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
983 ok(pad == sum, "padding %u prior to start\n", pad);
985 hr = IAudioClock_GetPosition(acl, &pos, NULL);
986 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
987 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
989 hr = IAudioClient_Start(ac); /* #1 */
990 ok(hr == S_OK, "Start failed: %08x\n", hr);
992 Sleep(100);
993 slept += 100;
995 hr = IAudioClient_GetStreamLatency(ac, &t1);
996 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
997 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
999 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1000 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1001 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1002 /* in rare cases is slept*1.1 not enough with dmix */
1003 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1004 last = pos;
1006 hr = IAudioClient_Stop(ac);
1007 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1009 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1010 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1011 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1012 last = pos;
1013 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1015 hr = IAudioClient_Start(ac); /* #2 */
1016 ok(hr == S_OK, "Start failed: %08x\n", hr);
1018 Sleep(100);
1019 slept += 100;
1021 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1022 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1023 trace("padding %u past sleep #2\n", pad);
1025 /** IAudioClient_Stop
1026 * Exclusive mode: the audio engine appears to drop frames,
1027 * bumping GetPosition to a higher value than time allows, even
1028 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1029 * Shared mode: no drop observed (or too small to be visible).
1030 * GetPosition = sum Released - GetCurrentPadding
1031 * Bugs: Some USB headset system drained the whole buffer, leaving
1032 * padding 0 and bumping pos to sum minus 17 frames! */
1034 hr = IAudioClient_Stop(ac);
1035 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1037 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1038 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1040 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1041 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1042 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1043 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1044 /* Prove that Stop must not drop frames (in shared mode). */
1045 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1046 if (share && pad > 0)
1047 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1048 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1049 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1050 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1051 last = pos;
1053 Sleep(100);
1054 slept += 100;
1056 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1057 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1058 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1060 /* Restart from 0 */
1061 hr = IAudioClient_Reset(ac);
1062 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1063 slept = sum = 0;
1065 hr = IAudioClient_Reset(ac);
1066 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1068 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1069 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1070 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1071 ok(pcpos > pcpos0, "pcpos should increase\n");
1073 avail = gbsize; /* implies GetCurrentPadding == 0 */
1074 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1075 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1076 trace("data at %p\n", data);
1078 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_interactive ?
1079 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1080 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1081 if(hr == S_OK) sum += avail;
1083 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1084 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1085 ok(pad == sum, "padding %u prior to start\n", pad);
1087 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1088 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1089 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1090 last = pos;
1092 hr = IAudioClient_Start(ac); /* #3 */
1093 ok(hr == S_OK, "Start failed: %08x\n", hr);
1095 Sleep(100);
1096 slept += 100;
1098 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1099 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1100 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1101 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1102 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1103 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1104 last = pos;
1106 hr = IAudioClient_Reset(ac);
1107 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1109 hr = IAudioClient_Stop(ac);
1110 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1112 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1113 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1115 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1116 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1117 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1118 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1119 ok(pcpos > pcpos0, "pcpos should increase\n");
1120 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1121 if (pad > 0)
1122 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1123 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1124 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1125 last = pos;
1127 /* Begin the big loop */
1128 hr = IAudioClient_Reset(ac);
1129 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1130 slept = last = sum = 0;
1131 pcpos0 = pcpos;
1133 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1135 hr = IAudioClient_Reset(ac);
1136 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1138 hr = IAudioClient_Start(ac);
1139 ok(hr == S_OK, "Start failed: %08x\n", hr);
1141 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1142 data = NULL;
1143 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1144 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1145 trace("data at %p for prefill %u\n", data, avail);
1147 hr = IAudioClient_Stop(ac);
1148 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1150 Sleep(20);
1151 slept += 20;
1153 hr = IAudioClient_Reset(ac);
1154 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1156 hr = IAudioClient_Start(ac);
1157 ok(hr == S_OK, "Start failed: %08x\n", hr);
1159 /* Despite passed time, data must still point to valid memory... */
1160 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1161 wave_generate_tone(pwfx, data, avail));
1162 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1163 if(hr == S_OK) sum += avail;
1165 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1166 * mixer may still have a little data. We believe an underrun will occur
1167 * when the mixer finds GCP smaller than a period size at the *end* of a
1168 * period cycle, i.e. shortly before calling SetEvent to signal the app
1169 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1170 * with no data written for over a period causes an underrun. */
1172 Sleep(350);
1173 slept += 350;
1174 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1175 trace("hpctime %u after %ums\n",
1176 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1178 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1179 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1180 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1181 last = pos;
1183 for(i=0; i < 9; i++) {
1184 Sleep(100);
1185 slept += 100;
1187 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1188 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1190 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1191 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1193 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1194 trace("hpctime %u pcpos %u\n",
1195 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1196 (ULONG)((pcpos-pcpos0)/10000));
1198 /* Use sum-pad to see whether position is ahead padding or not. */
1199 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1200 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1201 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1203 /* Padding does not lag behind by much */
1204 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1205 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1206 if (pad) /* not in case of underrun */
1207 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1208 "Position delta %ld not regular\n", (long)(pos-last));
1209 last = pos;
1211 hr = IAudioClient_GetStreamLatency(ac, &t1);
1212 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1213 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1215 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1216 data = NULL;
1217 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1218 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1219 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1220 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1221 if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1222 /* Only the first iteration should allow that large a buffer
1223 * as prefill was drained during the first 350+100ms sleep.
1224 * Afterwards, only 100ms of data should find room per iteration. */
1226 if(hr == S_OK) {
1227 trace("data at %p\n", data);
1228 } else {
1229 avail = gbsize - pad;
1230 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1231 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1232 trace("data at %p (small %u)\n", data, avail);
1234 ok(data != NULL, "NULL buffer returned\n");
1235 if(i % 3 && !winetest_interactive) {
1236 memset(data, 0, avail * pwfx->nBlockAlign);
1237 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1238 } else {
1239 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1240 wave_generate_tone(pwfx, data, avail));
1242 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1243 if(hr == S_OK) sum += avail;
1246 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1247 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1248 trace("position %u\n", (UINT)pos);
1250 Sleep(1000); /* 500ms buffer underrun past full buffer */
1252 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1253 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1255 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1256 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1257 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1259 if (share) {
1260 /* Following underrun, all samples were played */
1261 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1262 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1263 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1264 } else {
1265 /* Vista and w2k8 leave partial fragments behind */
1266 ok(pad == 0 /* w7, w2k8R2 */||
1267 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1268 /* expect at most 5 fragments (75ms) away */
1269 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1270 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1271 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1274 hr = IAudioClient_GetStreamLatency(ac, &t1);
1275 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1276 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1278 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1279 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1281 hr = IAudioClient_Stop(ac);
1282 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1284 CoTaskMemFree(pwfx);
1286 IAudioClock_Release(acl);
1287 IAudioRenderClient_Release(arc);
1288 IAudioClient_Release(ac);
1291 static void test_session(void)
1293 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1294 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1295 IMMDevice *cap_dev;
1296 GUID ses1_guid;
1297 AudioSessionState state;
1298 WAVEFORMATEX *pwfx;
1299 ULONG ref;
1300 HRESULT hr;
1302 hr = CoCreateGuid(&ses1_guid);
1303 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1305 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1306 NULL, (void**)&ses1_ac1);
1307 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1308 if (FAILED(hr)) return;
1310 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1311 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1313 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1314 0, 5000000, 0, pwfx, &ses1_guid);
1315 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1317 if(hr == S_OK){
1318 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1319 NULL, (void**)&ses1_ac2);
1320 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1322 if(hr != S_OK){
1323 skip("Unable to open the same device twice. Skipping session tests\n");
1325 ref = IAudioClient_Release(ses1_ac1);
1326 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1327 CoTaskMemFree(pwfx);
1328 return;
1331 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1332 0, 5000000, 0, pwfx, &ses1_guid);
1333 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1335 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1336 eMultimedia, &cap_dev);
1337 if(hr == S_OK){
1338 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1339 NULL, (void**)&cap_ac);
1340 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1341 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1342 IMMDevice_Release(cap_dev);
1344 if(hr == S_OK){
1345 WAVEFORMATEX *cap_pwfx;
1347 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1348 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1350 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1351 0, 5000000, 0, cap_pwfx, &ses1_guid);
1352 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1353 CoTaskMemFree(cap_pwfx);
1355 if(hr == S_OK){
1356 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1357 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1358 if(FAILED(hr))
1359 cap_ctl = NULL;
1360 }else
1361 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1363 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1364 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1366 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1367 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1369 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1370 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1371 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1372 ref = IAudioSessionControl_Release(ses1_ctl2);
1373 ok(ref != 0, "AudioSessionControl was destroyed\n");
1375 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1376 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1378 hr = IAudioSessionControl_GetState(ses1_ctl, NULL);
1379 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1381 hr = IAudioSessionControl_GetState(ses1_ctl, &state);
1382 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1383 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1385 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1386 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1387 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1389 if(cap_ctl){
1390 hr = IAudioSessionControl_GetState(cap_ctl, &state);
1391 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1392 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1395 hr = IAudioClient_Start(ses1_ac1);
1396 ok(hr == S_OK, "Start failed: %08x\n", hr);
1398 hr = IAudioSessionControl_GetState(ses1_ctl, &state);
1399 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1400 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1402 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1403 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1404 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1406 if(cap_ctl){
1407 hr = IAudioSessionControl_GetState(cap_ctl, &state);
1408 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1409 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1412 hr = IAudioClient_Stop(ses1_ac1);
1413 ok(hr == S_OK, "Start failed: %08x\n", hr);
1415 hr = IAudioSessionControl_GetState(ses1_ctl, &state);
1416 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1417 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1419 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1420 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1421 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1423 if(cap_ctl){
1424 hr = IAudioSessionControl_GetState(cap_ctl, &state);
1425 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1426 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1428 hr = IAudioClient_Start(cap_ac);
1429 ok(hr == S_OK, "Start failed: %08x\n", hr);
1431 hr = IAudioSessionControl_GetState(ses1_ctl, &state);
1432 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1433 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1435 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1436 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1437 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1439 hr = IAudioSessionControl_GetState(cap_ctl, &state);
1440 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1441 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1443 hr = IAudioClient_Stop(cap_ac);
1444 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1446 hr = IAudioSessionControl_GetState(ses1_ctl, &state);
1447 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1448 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1450 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1451 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1452 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1454 hr = IAudioSessionControl_GetState(cap_ctl, &state);
1455 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1456 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1458 ref = IAudioSessionControl_Release(cap_ctl);
1459 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1461 ref = IAudioClient_Release(cap_ac);
1462 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1465 ref = IAudioSessionControl_Release(ses1_ctl);
1466 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1468 ref = IAudioClient_Release(ses1_ac1);
1469 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1471 ref = IAudioClient_Release(ses1_ac2);
1472 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1474 /* we've released all of our IAudioClient references, so check GetState */
1475 hr = IAudioSessionControl_GetState(ses1_ctl2, &state);
1476 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1477 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1479 ref = IAudioSessionControl_Release(ses1_ctl2);
1480 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1482 CoTaskMemFree(pwfx);
1485 static void test_streamvolume(void)
1487 IAudioClient *ac;
1488 IAudioStreamVolume *asv;
1489 WAVEFORMATEX *fmt;
1490 UINT32 chans, i;
1491 HRESULT hr;
1492 float vol, *vols;
1494 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1495 NULL, (void**)&ac);
1496 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1497 if(hr != S_OK)
1498 return;
1500 hr = IAudioClient_GetMixFormat(ac, &fmt);
1501 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1503 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1504 0, fmt, NULL);
1505 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1507 if(hr == S_OK){
1508 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1509 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1511 if(hr != S_OK){
1512 IAudioClient_Release(ac);
1513 CoTaskMemFree(fmt);
1514 return;
1517 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1518 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1520 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1521 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1522 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1524 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1525 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1527 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1528 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1530 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1531 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1533 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1534 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1535 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1537 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1538 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1540 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1541 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1543 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1544 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1546 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1547 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1549 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1550 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1551 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1553 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1554 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1556 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1557 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1559 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1560 ok(vols != NULL, "HeapAlloc failed\n");
1562 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1563 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1565 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1566 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1567 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1568 for(i = 1; i < fmt->nChannels; ++i)
1569 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1571 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1572 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1574 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1575 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1577 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1578 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1580 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1581 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1583 HeapFree(GetProcessHeap(), 0, vols);
1584 IAudioStreamVolume_Release(asv);
1585 IAudioClient_Release(ac);
1586 CoTaskMemFree(fmt);
1589 static void test_channelvolume(void)
1591 IAudioClient *ac;
1592 IChannelAudioVolume *acv;
1593 WAVEFORMATEX *fmt;
1594 UINT32 chans, i;
1595 HRESULT hr;
1596 float vol, *vols;
1598 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1599 NULL, (void**)&ac);
1600 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1601 if(hr != S_OK)
1602 return;
1604 hr = IAudioClient_GetMixFormat(ac, &fmt);
1605 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1607 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1608 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1609 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1611 if(hr == S_OK){
1612 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1613 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1615 if(hr != S_OK){
1616 IAudioClient_Release(ac);
1617 CoTaskMemFree(fmt);
1618 return;
1621 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1622 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1624 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1625 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1626 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1628 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1629 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1631 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1632 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1634 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1635 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1637 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1638 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1639 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1641 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1642 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1644 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1645 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1647 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1648 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1650 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1651 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1653 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1654 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1655 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1657 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1658 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1660 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1661 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1663 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1664 ok(vols != NULL, "HeapAlloc failed\n");
1666 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1667 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1669 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1670 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1671 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1672 for(i = 1; i < fmt->nChannels; ++i)
1673 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1675 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1676 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1678 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1679 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1681 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1682 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1684 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1685 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1687 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1688 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1690 HeapFree(GetProcessHeap(), 0, vols);
1691 IChannelAudioVolume_Release(acv);
1692 IAudioClient_Release(ac);
1693 CoTaskMemFree(fmt);
1696 static void test_simplevolume(void)
1698 IAudioClient *ac;
1699 ISimpleAudioVolume *sav;
1700 WAVEFORMATEX *fmt;
1701 HRESULT hr;
1702 float vol;
1703 BOOL mute;
1705 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1706 NULL, (void**)&ac);
1707 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1708 if(hr != S_OK)
1709 return;
1711 hr = IAudioClient_GetMixFormat(ac, &fmt);
1712 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1714 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1715 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1716 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1718 if(hr == S_OK){
1719 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1720 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1722 if(hr != S_OK){
1723 IAudioClient_Release(ac);
1724 CoTaskMemFree(fmt);
1725 return;
1728 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1729 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1731 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1732 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1733 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1735 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1736 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1738 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1739 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1741 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1742 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1744 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1745 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1746 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1748 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1749 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1751 mute = TRUE;
1752 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1753 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1754 ok(mute == FALSE, "Session is already muted\n");
1756 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1757 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1759 mute = FALSE;
1760 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1761 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1762 ok(mute == TRUE, "Session should have been muted\n");
1764 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1765 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1766 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1768 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1769 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1771 mute = FALSE;
1772 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1773 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1774 ok(mute == TRUE, "Session should have been muted\n");
1776 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1777 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1779 ISimpleAudioVolume_Release(sav);
1780 IAudioClient_Release(ac);
1781 CoTaskMemFree(fmt);
1784 static void test_volume_dependence(void)
1786 IAudioClient *ac, *ac2;
1787 ISimpleAudioVolume *sav;
1788 IChannelAudioVolume *cav;
1789 IAudioStreamVolume *asv;
1790 WAVEFORMATEX *fmt;
1791 HRESULT hr;
1792 float vol;
1793 GUID session;
1794 UINT32 nch;
1796 hr = CoCreateGuid(&session);
1797 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1799 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1800 NULL, (void**)&ac);
1801 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1802 if(hr != S_OK)
1803 return;
1805 hr = IAudioClient_GetMixFormat(ac, &fmt);
1806 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1808 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1809 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1810 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1812 if(hr == S_OK){
1813 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1814 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1816 if(hr != S_OK){
1817 IAudioClient_Release(ac);
1818 CoTaskMemFree(fmt);
1819 return;
1822 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1823 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
1825 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1826 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1828 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1829 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1831 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1832 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1834 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1835 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1837 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1838 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1839 ok(fabsf(vol - 0.2) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1841 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1842 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1843 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1845 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1846 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1847 ok(fabsf(vol - 0.6) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1849 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1850 NULL, (void**)&ac2);
1851 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1853 if(hr == S_OK){
1854 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1855 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1856 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1857 if(hr != S_OK)
1858 IAudioClient_Release(ac2);
1861 if(hr == S_OK){
1862 IChannelAudioVolume *cav2;
1863 IAudioStreamVolume *asv2;
1865 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1866 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1868 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1869 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1871 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1872 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1873 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1875 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1876 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1877 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1879 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1880 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1881 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1883 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1884 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1885 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1887 IAudioStreamVolume_Release(asv2);
1888 IChannelAudioVolume_Release(cav2);
1889 IAudioClient_Release(ac2);
1890 }else
1891 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1893 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1894 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1896 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1897 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1899 CoTaskMemFree(fmt);
1900 ISimpleAudioVolume_Release(sav);
1901 IChannelAudioVolume_Release(cav);
1902 IAudioStreamVolume_Release(asv);
1903 IAudioClient_Release(ac);
1906 static void test_session_creation(void)
1908 IMMDevice *cap_dev;
1909 IAudioClient *ac;
1910 IAudioSessionManager *sesm;
1911 ISimpleAudioVolume *sav;
1912 GUID session_guid;
1913 float vol;
1914 HRESULT hr;
1915 WAVEFORMATEX *fmt;
1917 CoCreateGuid(&session_guid);
1919 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1920 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1921 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1922 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1924 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1925 FALSE, &sav);
1926 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1928 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1929 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1931 /* Release completely to show session persistence */
1932 ISimpleAudioVolume_Release(sav);
1933 IAudioSessionManager_Release(sesm);
1935 /* test if we can create a capture audioclient in the session we just
1936 * created from a SessionManager derived from a render device */
1937 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1938 eMultimedia, &cap_dev);
1939 if(hr == S_OK){
1940 WAVEFORMATEX *cap_pwfx;
1941 IAudioClient *cap_ac;
1942 ISimpleAudioVolume *cap_sav;
1943 IAudioSessionManager *cap_sesm;
1945 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1946 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1947 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1948 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1950 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1951 FALSE, &cap_sav);
1952 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1954 vol = 0.5f;
1955 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1956 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1957 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
1959 ISimpleAudioVolume_Release(cap_sav);
1960 IAudioSessionManager_Release(cap_sesm);
1962 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1963 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1964 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1966 IMMDevice_Release(cap_dev);
1968 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1969 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1971 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1972 0, 5000000, 0, cap_pwfx, &session_guid);
1973 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1975 CoTaskMemFree(cap_pwfx);
1977 if(hr == S_OK){
1978 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
1979 (void**)&cap_sav);
1980 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1982 if(hr == S_OK){
1983 vol = 0.5f;
1984 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1985 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1986 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
1988 ISimpleAudioVolume_Release(cap_sav);
1991 IAudioClient_Release(cap_ac);
1994 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1995 NULL, (void**)&ac);
1996 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
1997 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1998 if(hr != S_OK)
1999 return;
2001 hr = IAudioClient_GetMixFormat(ac, &fmt);
2002 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2004 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2005 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2006 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2008 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2009 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2010 if(hr == S_OK){
2011 vol = 0.5f;
2012 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2013 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2014 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2016 ISimpleAudioVolume_Release(sav);
2019 CoTaskMemFree(fmt);
2020 IAudioClient_Release(ac);
2023 static void test_worst_case(void)
2025 HANDLE event;
2026 HRESULT hr;
2027 IAudioClient *ac;
2028 IAudioRenderClient *arc;
2029 IAudioClock *acl;
2030 WAVEFORMATEX *pwfx;
2031 REFERENCE_TIME defp;
2032 UINT64 freq, pos, pcpos0, pcpos;
2033 BYTE *data;
2034 DWORD r;
2035 UINT32 pad, fragment, sum;
2036 int i,j;
2038 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2039 NULL, (void**)&ac);
2040 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2041 if(hr != S_OK)
2042 return;
2044 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2045 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2047 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2048 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2049 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2050 if(hr != S_OK)
2051 return;
2053 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2054 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2056 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2058 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2059 ok(event != NULL, "CreateEvent failed\n");
2061 hr = IAudioClient_SetEventHandle(ac, event);
2062 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2064 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2065 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2067 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2068 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2070 hr = IAudioClock_GetFrequency(acl, &freq);
2071 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2073 for(j = 0; j < 10; j++){
2074 sum = 0;
2075 trace("Should play %ums continuous tone with fragment size %u.\n",
2076 (ULONG)(defp/100), fragment);
2078 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2079 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2081 /* XAudio2 prefills one period, play without it */
2082 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2083 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2085 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2086 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2087 if(hr == S_OK)
2088 sum += fragment;
2090 hr = IAudioClient_Start(ac);
2091 ok(hr == S_OK, "Start failed: %08x\n", hr);
2093 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2094 r = WaitForSingleObject(event, 60 + defp / 10000);
2095 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2097 /* the app has nearly one period time to feed data */
2098 Sleep((i % 10) * defp / 120000);
2100 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2101 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2103 /* XAudio2 writes only when there's little data left */
2104 if(pad <= fragment){
2105 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2106 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2108 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2109 wave_generate_tone(pwfx, data, fragment));
2110 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2111 if(hr == S_OK)
2112 sum += fragment;
2116 hr = IAudioClient_Stop(ac);
2117 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2119 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2120 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2122 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2123 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2125 Sleep(100);
2127 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2128 sum, sum/fragment, fragment, pad,
2129 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2130 (ULONG)((pcpos-pcpos0)/10000));
2132 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2133 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2135 hr = IAudioClient_Reset(ac);
2136 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2138 Sleep(250);
2141 CoTaskMemFree(pwfx);
2142 IAudioClient_Release(ac);
2143 IAudioClock_Release(acl);
2144 IAudioRenderClient_Release(arc);
2147 START_TEST(render)
2149 HRESULT hr;
2151 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2152 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2153 if (FAILED(hr))
2155 skip("mmdevapi not available: 0x%08x\n", hr);
2156 goto cleanup;
2159 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2160 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2161 if (hr != S_OK || !dev)
2163 if (hr == E_NOTFOUND)
2164 skip("No sound card available\n");
2165 else
2166 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2167 goto cleanup;
2170 test_audioclient();
2171 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2172 test_formats(AUDCLNT_SHAREMODE_SHARED);
2173 test_references();
2174 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2175 trace("Please redirect output to a file.\n");
2176 test_event();
2177 test_padding();
2178 test_clock(1);
2179 test_clock(0);
2180 test_session();
2181 test_streamvolume();
2182 test_channelvolume();
2183 test_simplevolume();
2184 test_volume_dependence();
2185 test_session_creation();
2186 test_worst_case();
2188 IMMDevice_Release(dev);
2190 cleanup:
2191 if (mme)
2192 IMMDeviceEnumerator_Release(mme);
2193 CoUninitialize();