qmgr: Update to IBackgroundCopyFile2.
[wine.git] / dlls / mmdevapi / tests / render.c
blob3a48c8d24004660ef32b32f13238df9e22ba3775
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 "mmsystem.h"
40 #include "audioclient.h"
41 #include "audiopolicy.h"
43 static const unsigned int win_formats[][4] = {
44 { 8000, 8, 1}, { 8000, 8, 2}, { 8000, 16, 1}, { 8000, 16, 2},
45 {11025, 8, 1}, {11025, 8, 2}, {11025, 16, 1}, {11025, 16, 2},
46 {12000, 8, 1}, {12000, 8, 2}, {12000, 16, 1}, {12000, 16, 2},
47 {16000, 8, 1}, {16000, 8, 2}, {16000, 16, 1}, {16000, 16, 2},
48 {22050, 8, 1}, {22050, 8, 2}, {22050, 16, 1}, {22050, 16, 2},
49 {44100, 8, 1}, {44100, 8, 2}, {44100, 16, 1}, {44100, 16, 2},
50 {48000, 8, 1}, {48000, 8, 2}, {48000, 16, 1}, {48000, 16, 2},
51 {96000, 8, 1}, {96000, 8, 2}, {96000, 16, 1}, {96000, 16, 2}
53 #define NB_WIN_FORMATS (sizeof(win_formats)/sizeof(*win_formats))
55 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
57 static IMMDeviceEnumerator *mme = NULL;
58 static IMMDevice *dev = NULL;
59 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
61 static const LARGE_INTEGER ullZero;
63 #define PI 3.14159265358979323846L
64 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
66 static double phase = 0.; /* normalized to unit, not 2*PI */
67 PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
68 DWORD cn, i;
69 double delta, y;
71 if(!winetest_interactive)
72 return AUDCLNT_BUFFERFLAGS_SILENT;
73 if(wfxe->Format.wBitsPerSample != ((wfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
74 IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) ? 8 * sizeof(float) : 16))
75 return AUDCLNT_BUFFERFLAGS_SILENT;
77 for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
78 delta += .5/wfxe->Format.nChannels, cn++){
79 for(i = 0; i < frames; i++){
80 y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
81 /* assume alignment is granted */
82 if(wfxe->Format.wBitsPerSample == 16)
83 ((short*)data)[cn+i*wfxe->Format.nChannels] = y * 32767.9;
84 else
85 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
88 phase += 440.* frames / wfxe->Format.nSamplesPerSec;
89 phase -= floor(phase);
90 return 0;
93 static void test_uninitialized(IAudioClient *ac)
95 HRESULT hr;
96 UINT32 num;
97 REFERENCE_TIME t1;
99 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
100 IUnknown *unk;
102 hr = IAudioClient_GetBufferSize(ac, &num);
103 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
105 hr = IAudioClient_GetStreamLatency(ac, &t1);
106 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
108 hr = IAudioClient_GetCurrentPadding(ac, &num);
109 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
111 hr = IAudioClient_Start(ac);
112 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
114 hr = IAudioClient_Stop(ac);
115 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
117 hr = IAudioClient_Reset(ac);
118 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
120 hr = IAudioClient_SetEventHandle(ac, handle);
121 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
123 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
124 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
126 CloseHandle(handle);
129 static void test_audioclient(void)
131 IAudioClient *ac;
132 IUnknown *unk;
133 HRESULT hr;
134 ULONG ref;
135 WAVEFORMATEX *pwfx, *pwfx2;
136 REFERENCE_TIME t1, t2;
137 HANDLE handle;
139 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
140 NULL, (void**)&ac);
141 ok(hr == S_OK, "Activation failed with %08x\n", hr);
142 if(hr != S_OK)
143 return;
145 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
147 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
148 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
150 unk = (void*)(LONG_PTR)0x12345678;
151 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
152 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
153 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
155 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
156 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
157 if (unk)
159 ref = IUnknown_Release(unk);
160 ok(ref == 1, "Released count is %u\n", ref);
163 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
164 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
165 if (unk)
167 ref = IUnknown_Release(unk);
168 ok(ref == 1, "Released count is %u\n", ref);
171 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
172 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
174 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
175 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
177 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
178 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
180 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
181 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
182 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
183 (UINT)(t1/10000), (UINT)(t1 % 10000),
184 (UINT)(t2/10000), (UINT)(t2 % 10000));
186 hr = IAudioClient_GetMixFormat(ac, NULL);
187 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
189 hr = IAudioClient_GetMixFormat(ac, &pwfx);
190 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
192 if (hr == S_OK)
194 trace("pwfx: %p\n", pwfx);
195 trace("Tag: %04x\n", pwfx->wFormatTag);
196 trace("bits: %u\n", pwfx->wBitsPerSample);
197 trace("chan: %u\n", pwfx->nChannels);
198 trace("rate: %u\n", pwfx->nSamplesPerSec);
199 trace("align: %u\n", pwfx->nBlockAlign);
200 trace("extra: %u\n", pwfx->cbSize);
201 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
202 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
204 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
205 trace("Res: %u\n", pwfxe->Samples.wReserved);
206 trace("Mask: %x\n", pwfxe->dwChannelMask);
207 trace("Alg: %s\n",
208 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
209 (IsEqualGUID(&pwfxe->SubFormat,
210 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
213 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
214 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
215 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
216 CoTaskMemFree(pwfx2);
218 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
219 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
221 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
222 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
224 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
225 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
226 "IsFormatSupported(Exclusive) call returns %08x\n", hr);
227 hexcl = hr;
229 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
230 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
231 ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
232 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
234 if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
235 hexcl = S_OK;
237 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
238 ok(hr == E_INVALIDARG/*w32*/ ||
239 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
240 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
243 test_uninitialized(ac);
245 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
246 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
248 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
249 ok(hr == E_INVALIDARG ||
250 hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
252 /* A period != 0 is ignored and the call succeeds.
253 * Since we can only initialize successfully once, skip those tests.
255 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
256 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
258 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
259 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
260 if(hr == S_OK){
261 UINT32 num;
263 hr = IAudioClient_GetBufferSize(ac, &num);
264 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
265 if(hr == S_OK)
266 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
269 IAudioClient_Release(ac);
271 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
272 NULL, (void**)&ac);
273 ok(hr == S_OK, "Activation failed with %08x\n", hr);
275 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
276 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
277 WAVEFORMATEX *fmt2 = NULL;
279 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
281 fmtex->dwChannelMask = 0xffff;
283 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
284 ok(hr == S_OK, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
286 IAudioClient_Release(ac);
288 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
289 NULL, (void**)&ac);
290 ok(hr == S_OK, "Activation failed with %08x\n", hr);
292 fmtex->dwChannelMask = 0;
294 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
295 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
296 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
297 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
299 CoTaskMemFree(fmt2);
301 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
302 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
304 IAudioClient_Release(ac);
306 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
307 NULL, (void**)&ac);
308 ok(hr == S_OK, "Activation failed with %08x\n", hr);
310 CoTaskMemFree(pwfx);
312 hr = IAudioClient_GetMixFormat(ac, &pwfx);
313 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
314 }else
315 skip("Skipping dwChannelMask tests\n");
317 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
318 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
319 if (hr != S_OK)
321 IAudioClient_Release(ac);
322 CoTaskMemFree(pwfx);
323 return;
326 hr = IAudioClient_GetStreamLatency(ac, NULL);
327 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
329 hr = IAudioClient_GetStreamLatency(ac, &t2);
330 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
331 trace("Returned latency: %u.%04u ms\n",
332 (UINT)(t2/10000), (UINT)(t2 % 10000));
333 ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000),
334 "Latency < default period, delta %ldus\n", (long)((t2-t1)/10));
335 /* Native appears to add the engine period to the HW latency in shared mode */
337 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
338 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
340 hr = IAudioClient_SetEventHandle(ac, NULL);
341 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
343 hr = IAudioClient_SetEventHandle(ac, handle);
344 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
345 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
346 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
347 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
348 , "SetEventHandle returns %08x\n", hr);
350 hr = IAudioClient_Reset(ac);
351 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
353 hr = IAudioClient_Reset(ac);
354 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
356 hr = IAudioClient_Stop(ac);
357 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
359 hr = IAudioClient_Start(ac);
360 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
362 hr = IAudioClient_Start(ac);
363 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
365 IAudioClient_Release(ac);
367 CloseHandle(handle);
368 CoTaskMemFree(pwfx);
371 static void test_formats(AUDCLNT_SHAREMODE mode)
373 IAudioClient *ac;
374 HRESULT hr, hrs;
375 WAVEFORMATEX fmt, *pwfx, *pwfx2;
376 int i;
378 fmt.wFormatTag = WAVE_FORMAT_PCM;
379 fmt.cbSize = 0;
381 for(i = 0; i < NB_WIN_FORMATS; i++) {
382 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
383 NULL, (void**)&ac);
384 ok(hr == S_OK, "Activation failed with %08x\n", hr);
385 if(hr != S_OK)
386 continue;
388 hr = IAudioClient_GetMixFormat(ac, &pwfx);
389 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
391 fmt.nSamplesPerSec = win_formats[i][0];
392 fmt.wBitsPerSample = win_formats[i][1];
393 fmt.nChannels = win_formats[i][2];
394 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
395 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
397 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
398 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
399 hrs = hr;
400 /* Only shared mode suggests something ... GetMixFormat! */
401 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
402 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
403 /* 5:1 card exception when asked for 1 channel at mixer rate */
404 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
405 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
406 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
407 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
408 if (hr == S_OK)
409 trace("IsSupported(%s, %ux%2ux%u)\n",
410 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
411 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
413 /* Change GetMixFormat wBitsPerSample only => S_OK */
414 if (mode == AUDCLNT_SHAREMODE_SHARED
415 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
416 && fmt.nChannels == pwfx->nChannels)
417 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
419 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
420 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
421 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
422 if (pwfx2) {
423 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
424 pwfx2->nChannels == pwfx->nChannels &&
425 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
426 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
427 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
430 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
431 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
432 if ((hrs == S_OK) ^ (hr == S_OK))
433 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
434 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
435 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
436 if (mode == AUDCLNT_SHAREMODE_SHARED)
437 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
438 "Initialize(shared, %ux%2ux%u) returns %08x\n",
439 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
440 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
441 /* Unsupported format implies "create failed" and shadows "not allowed" */
442 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
443 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
444 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
445 else
446 /* On testbot 48000x16x1 claims support, but does not Initialize.
447 * Some cards Initialize 44100|48000x16x1 yet claim no support;
448 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
449 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
450 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == AUDCLNT_E_UNSUPPORTED_FORMAT ||
451 broken(hr == S_OK &&
452 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
453 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
454 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
455 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
457 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
458 * Release this ac and Activate a new one.
459 * A second call (with a known working format) would yield
460 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
461 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
462 * all subsequent calls until the audio engine service is restarted. */
464 CoTaskMemFree(pwfx2);
465 CoTaskMemFree(pwfx);
466 IAudioClient_Release(ac);
470 static void test_references(void)
472 IAudioClient *ac;
473 IAudioRenderClient *rc;
474 ISimpleAudioVolume *sav;
475 IAudioStreamVolume *asv;
476 IAudioClock *acl;
477 WAVEFORMATEX *pwfx;
478 HRESULT hr;
479 ULONG ref;
481 /* IAudioRenderClient */
482 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
483 NULL, (void**)&ac);
484 ok(hr == S_OK, "Activation failed with %08x\n", hr);
485 if(hr != S_OK)
486 return;
488 hr = IAudioClient_GetMixFormat(ac, &pwfx);
489 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
491 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
492 0, pwfx, NULL);
493 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
495 CoTaskMemFree(pwfx);
497 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
498 ok(hr == S_OK, "GetService failed: %08x\n", hr);
499 if(hr != S_OK) {
500 IAudioClient_Release(ac);
501 return;
504 IAudioRenderClient_AddRef(rc);
505 ref = IAudioRenderClient_Release(rc);
506 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
508 ref = IAudioClient_Release(ac);
509 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
511 ref = IAudioRenderClient_Release(rc);
512 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
514 /* ISimpleAudioVolume */
515 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
516 NULL, (void**)&ac);
517 ok(hr == S_OK, "Activation failed with %08x\n", hr);
518 if(hr != S_OK)
519 return;
521 hr = IAudioClient_GetMixFormat(ac, &pwfx);
522 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
524 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
525 0, pwfx, NULL);
526 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
528 CoTaskMemFree(pwfx);
530 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
531 ok(hr == S_OK, "GetService failed: %08x\n", hr);
533 ISimpleAudioVolume_AddRef(sav);
534 ref = ISimpleAudioVolume_Release(sav);
535 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
537 ref = IAudioClient_Release(ac);
538 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
540 ref = ISimpleAudioVolume_Release(sav);
541 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
543 /* IAudioClock */
544 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
545 NULL, (void**)&ac);
546 ok(hr == S_OK, "Activation failed with %08x\n", hr);
547 if(hr != S_OK)
548 return;
550 hr = IAudioClient_GetMixFormat(ac, &pwfx);
551 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
553 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
554 0, pwfx, NULL);
555 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
557 CoTaskMemFree(pwfx);
559 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
560 ok(hr == S_OK, "GetService failed: %08x\n", hr);
562 IAudioClock_AddRef(acl);
563 ref = IAudioClock_Release(acl);
564 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
566 ref = IAudioClient_Release(ac);
567 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
569 ref = IAudioClock_Release(acl);
570 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
572 /* IAudioStreamVolume */
573 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
574 NULL, (void**)&ac);
575 ok(hr == S_OK, "Activation failed with %08x\n", hr);
576 if(hr != S_OK)
577 return;
579 hr = IAudioClient_GetMixFormat(ac, &pwfx);
580 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
582 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
583 0, pwfx, NULL);
584 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
586 CoTaskMemFree(pwfx);
588 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
589 ok(hr == S_OK, "GetService failed: %08x\n", hr);
591 IAudioStreamVolume_AddRef(asv);
592 ref = IAudioStreamVolume_Release(asv);
593 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
595 ref = IAudioClient_Release(ac);
596 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
598 ref = IAudioStreamVolume_Release(asv);
599 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
602 static void test_event(void)
604 HANDLE event;
605 HRESULT hr;
606 DWORD r;
607 IAudioClient *ac;
608 WAVEFORMATEX *pwfx;
610 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
611 NULL, (void**)&ac);
612 ok(hr == S_OK, "Activation failed with %08x\n", hr);
613 if(hr != S_OK)
614 return;
616 hr = IAudioClient_GetMixFormat(ac, &pwfx);
617 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
619 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
620 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
621 0, pwfx, NULL);
622 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
624 CoTaskMemFree(pwfx);
626 event = CreateEventW(NULL, FALSE, FALSE, NULL);
627 ok(event != NULL, "CreateEvent failed\n");
629 hr = IAudioClient_Start(ac);
630 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start failed: %08x\n", hr);
632 hr = IAudioClient_SetEventHandle(ac, event);
633 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
635 hr = IAudioClient_SetEventHandle(ac, event);
636 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), "SetEventHandle returns %08x\n", hr);
638 r = WaitForSingleObject(event, 40);
639 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
641 hr = IAudioClient_Start(ac);
642 ok(hr == S_OK, "Start failed: %08x\n", hr);
644 r = WaitForSingleObject(event, 20);
645 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
647 hr = IAudioClient_Stop(ac);
648 ok(hr == S_OK, "Stop failed: %08x\n", hr);
650 ok(ResetEvent(event), "ResetEvent\n");
652 /* Still receiving events! */
653 r = WaitForSingleObject(event, 20);
654 ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
656 hr = IAudioClient_Reset(ac);
657 ok(hr == S_OK, "Reset failed: %08x\n", hr);
659 ok(ResetEvent(event), "ResetEvent\n");
661 r = WaitForSingleObject(event, 120);
662 ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
664 hr = IAudioClient_SetEventHandle(ac, NULL);
665 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
667 r = WaitForSingleObject(event, 70);
668 ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
670 /* test releasing a playing stream */
671 hr = IAudioClient_Start(ac);
672 ok(hr == S_OK, "Start failed: %08x\n", hr);
673 IAudioClient_Release(ac);
675 CloseHandle(event);
678 static void test_padding(void)
680 HRESULT hr;
681 IAudioClient *ac;
682 IAudioRenderClient *arc;
683 WAVEFORMATEX *pwfx;
684 REFERENCE_TIME minp, defp;
685 BYTE *buf, silence;
686 UINT32 psize, pad, written, i;
688 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
689 NULL, (void**)&ac);
690 ok(hr == S_OK, "Activation failed with %08x\n", hr);
691 if(hr != S_OK)
692 return;
694 hr = IAudioClient_GetMixFormat(ac, &pwfx);
695 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
696 if(hr != S_OK)
697 return;
699 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
700 0, 5000000, 0, pwfx, NULL);
701 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
702 if(hr != S_OK)
703 return;
705 if(pwfx->wBitsPerSample == 8)
706 silence = 128;
707 else
708 silence = 0;
710 /** GetDevicePeriod
711 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
712 * except when the HW/OS forces a particular alignment,
713 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
714 * 441 observed with Vista, 448 with w7 on the same HW! */
715 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
716 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
717 /* some wineXYZ.drv use 20ms, not seen on native */
718 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
719 "Expected 10ms default period: %u\n", (ULONG)defp);
720 ok(minp != 0, "Minimum period is 0\n");
721 ok(minp <= defp, "Mininum period is greater than default period\n");
723 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
724 ok(hr == S_OK, "GetService failed: %08x\n", hr);
726 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
728 written = 0;
729 hr = IAudioClient_GetCurrentPadding(ac, &pad);
730 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
731 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
733 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
734 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
735 ok(buf != NULL, "NULL buffer returned\n");
736 for(i = 0; i < psize * pwfx->nBlockAlign; ++i){
737 if(buf[i] != silence){
738 ok(0, "buffer has data in it already\n");
739 break;
743 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
744 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
745 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
746 /* MSDN instead documents buf remains untouched */
748 hr = IAudioClient_Reset(ac);
749 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
751 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
752 AUDCLNT_BUFFERFLAGS_SILENT);
753 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
754 if(hr == S_OK) written += psize;
756 hr = IAudioClient_GetCurrentPadding(ac, &pad);
757 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
758 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
760 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
762 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
763 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
764 ok(buf != NULL, "NULL buffer returned\n");
766 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
767 AUDCLNT_BUFFERFLAGS_SILENT);
768 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
769 written += psize;
771 hr = IAudioClient_GetCurrentPadding(ac, &pad);
772 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
773 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
775 /* overfull buffer. requested 1/2s buffer size, so try
776 * to get a 1/2s buffer, which should fail */
777 psize = pwfx->nSamplesPerSec / 2;
778 buf = (void*)0xDEADF00D;
779 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
780 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
781 ok(buf == NULL, "NULL expected %p\n", buf);
783 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
784 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
786 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
788 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
789 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
790 ok(buf != NULL, "NULL buffer returned\n");
792 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
793 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
795 buf = (void*)0xDEADF00D;
796 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
797 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
798 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
799 /* MSDN instead documents buf remains untouched */
801 buf = (void*)0xDEADF00D;
802 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
803 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
804 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
806 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
807 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
809 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
810 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
811 ok(buf != NULL, "NULL buffer returned\n");
813 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
814 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
816 hr = IAudioClient_GetCurrentPadding(ac, &pad);
817 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
818 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
820 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
821 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
822 ok(buf != NULL, "NULL buffer returned\n");
824 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
825 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
826 /* todo_wine means Wine may overwrite memory */
827 if(hr == S_OK) written += psize+1;
829 /* Buffer still hold */
830 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
831 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
832 if(hr == S_OK) written += psize/2;
834 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
835 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
837 hr = IAudioClient_GetCurrentPadding(ac, &pad);
838 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
839 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
841 CoTaskMemFree(pwfx);
843 IAudioRenderClient_Release(arc);
844 IAudioClient_Release(ac);
847 static void test_clock(int share)
849 HRESULT hr;
850 IAudioClient *ac;
851 IAudioClock *acl;
852 IAudioRenderClient *arc;
853 UINT64 freq, pos, pcpos0, pcpos, last;
854 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
855 BYTE *data;
856 WAVEFORMATEX *pwfx;
857 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
858 REFERENCE_TIME minp, defp, t1, t2;
859 REFERENCE_TIME duration = 5000000, period = 150000;
860 int i;
862 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
864 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
865 NULL, (void**)&ac);
866 ok(hr == S_OK, "Activation failed with %08x\n", hr);
867 if(hr != S_OK)
868 return;
870 hr = IAudioClient_GetMixFormat(ac, &pwfx);
871 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
872 if(hr != S_OK)
873 return;
875 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
876 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
877 ok(minp <= period, "desired period %u too small for %u\n", (ULONG)period, (ULONG)minp);
879 if (share) {
880 trace("Testing shared mode\n");
881 /* period is ignored */
882 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
883 0, duration, period, pwfx, NULL);
884 period = defp;
885 } else {
886 pwfx->wFormatTag = WAVE_FORMAT_PCM;
887 pwfx->nChannels = 2;
888 pwfx->cbSize = 0;
889 pwfx->wBitsPerSample = 16; /* no floating point */
890 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
891 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
892 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
894 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
895 0, duration, period, pwfx, NULL);
897 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
898 if (hr != S_OK) {
899 CoTaskMemFree(pwfx);
900 IAudioClient_Release(ac);
901 if(hr == AUDCLNT_E_DEVICE_IN_USE)
902 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
903 return;
906 /** GetStreamLatency
907 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
908 * Exclusive mode: testbot returns 2x period + a little, but
909 * some HDA drivers return 1x period, some + a little. */
910 hr = IAudioClient_GetStreamLatency(ac, &t2);
911 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
912 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
913 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000),
914 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
916 /** GetBufferSize
917 * BufferSize must be rounded up, maximum 2s says MSDN.
918 * Both is wrong. Rounding may lead to size a little smaller than duration;
919 * duration > 2s is accepted in shared mode.
920 * Shared mode: round solely w.r.t. mixer rate,
921 * duration is no multiple of period.
922 * Exclusive mode: size appears as a multiple of some fragment that
923 * is either the rounded period or a fixed constant like 1024,
924 * whatever the driver implements. */
925 hr = IAudioClient_GetBufferSize(ac, &gbsize);
926 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
928 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
929 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
930 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
931 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
932 /* fragment size (= period in frames) is rounded up.
933 * BufferSize must be rounded up, maximum 2s says MSDN
934 * but it is rounded down modulo fragment ! */
935 if (share)
936 ok(gbsize == bufsize,
937 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
938 else
939 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
940 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
942 /* In shared mode, GetCurrentPadding decreases in multiples of
943 * fragment size (i.e. updated only at period ticks), whereas
944 * GetPosition appears to be reporting continuous positions.
945 * In exclusive mode, testbot behaves likewise, but native's Intel
946 * HDA driver shows no such deltas, GetCurrentPadding closely
947 * matches GetPosition, as in
948 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
950 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
951 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
953 hr = IAudioClock_GetFrequency(acl, &freq);
954 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
955 trace("Clock Frequency %u\n", (UINT)freq);
957 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
958 if (share)
959 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
960 "Clock Frequency %u\n", (UINT)freq);
961 else
962 ok(freq == pwfx->nSamplesPerSec,
963 "Clock Frequency %u\n", (UINT)freq);
965 hr = IAudioClock_GetPosition(acl, NULL, NULL);
966 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
968 pcpos0 = 0;
969 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
970 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
971 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
972 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
974 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
975 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
977 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
978 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
980 avail = gbsize;
981 data = NULL;
982 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
983 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
984 trace("data at %p\n", data);
986 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
987 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
988 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
989 if(hr == S_OK) sum += avail;
991 hr = IAudioClient_GetCurrentPadding(ac, &pad);
992 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
993 ok(pad == sum, "padding %u prior to start\n", pad);
995 hr = IAudioClock_GetPosition(acl, &pos, NULL);
996 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
997 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
999 hr = IAudioClient_Start(ac); /* #1 */
1000 ok(hr == S_OK, "Start failed: %08x\n", hr);
1002 Sleep(100);
1003 slept += 100;
1005 hr = IAudioClient_GetStreamLatency(ac, &t1);
1006 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1007 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1009 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1010 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1011 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1012 /* in rare cases is slept*1.1 not enough with dmix */
1013 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1014 last = pos;
1016 hr = IAudioClient_Stop(ac);
1017 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1019 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1020 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1021 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1022 last = pos;
1023 if(/*share &&*/ winetest_debug>1)
1024 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1026 hr = IAudioClient_Start(ac); /* #2 */
1027 ok(hr == S_OK, "Start failed: %08x\n", hr);
1029 Sleep(100);
1030 slept += 100;
1032 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1033 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1034 trace("padding %u past sleep #2\n", pad);
1036 /** IAudioClient_Stop
1037 * Exclusive mode: the audio engine appears to drop frames,
1038 * bumping GetPosition to a higher value than time allows, even
1039 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1040 * Shared mode: no drop observed (or too small to be visible).
1041 * GetPosition = sum Released - GetCurrentPadding
1042 * Bugs: Some USB headset system drained the whole buffer, leaving
1043 * padding 0 and bumping pos to sum minus 17 frames! */
1045 hr = IAudioClient_Stop(ac);
1046 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1048 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1049 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1051 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1052 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1053 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1054 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1055 /* Prove that Stop must not drop frames (in shared mode). */
1056 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1057 if (share && pad > 0 && winetest_debug>1)
1058 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1059 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1060 if(/*share &&*/ winetest_debug>1)
1061 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1062 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1063 last = pos;
1065 Sleep(100);
1067 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1068 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1069 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1071 /* Restart from 0 */
1072 hr = IAudioClient_Reset(ac);
1073 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1074 slept = sum = 0;
1076 hr = IAudioClient_Reset(ac);
1077 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1079 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1080 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1081 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1082 ok(pcpos > pcpos0, "pcpos should increase\n");
1084 avail = gbsize; /* implies GetCurrentPadding == 0 */
1085 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1086 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1087 trace("data at %p\n", data);
1089 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1090 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1091 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1092 if(hr == S_OK) sum += avail;
1094 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1095 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1096 ok(pad == sum, "padding %u prior to start\n", pad);
1098 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1099 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1100 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1101 last = pos;
1103 hr = IAudioClient_Start(ac); /* #3 */
1104 ok(hr == S_OK, "Start failed: %08x\n", hr);
1106 Sleep(100);
1107 slept += 100;
1109 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1110 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1111 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1112 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1113 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1114 if (winetest_debug>1)
1115 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1116 else
1117 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1118 last = pos;
1120 hr = IAudioClient_Reset(ac);
1121 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1123 hr = IAudioClient_Stop(ac);
1124 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1126 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1127 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1129 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1130 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1131 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1132 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1133 ok(pcpos > pcpos0, "pcpos should increase\n");
1134 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1135 if (pad > 0 && winetest_debug>1)
1136 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1137 if(winetest_debug>1)
1138 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1139 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1140 last = pos;
1142 /* Begin the big loop */
1143 hr = IAudioClient_Reset(ac);
1144 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1145 slept = last = sum = 0;
1146 pcpos0 = pcpos;
1148 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1150 hr = IAudioClient_Reset(ac);
1151 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1153 hr = IAudioClient_Start(ac);
1154 ok(hr == S_OK, "Start failed: %08x\n", hr);
1156 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1157 data = NULL;
1158 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1159 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1160 trace("data at %p for prefill %u\n", data, avail);
1162 if (winetest_debug>2) {
1163 hr = IAudioClient_Stop(ac);
1164 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1166 Sleep(20);
1167 slept += 20;
1169 hr = IAudioClient_Reset(ac);
1170 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1172 hr = IAudioClient_Start(ac);
1173 ok(hr == S_OK, "Start failed: %08x\n", hr);
1176 /* Despite passed time, data must still point to valid memory... */
1177 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1178 wave_generate_tone(pwfx, data, avail));
1179 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1180 if(hr == S_OK) sum += avail;
1182 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1183 * mixer may still have a little data. We believe an underrun will occur
1184 * when the mixer finds GCP smaller than a period size at the *end* of a
1185 * period cycle, i.e. shortly before calling SetEvent to signal the app
1186 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1187 * with no data written for over a period causes an underrun. */
1189 Sleep(350);
1190 slept += 350;
1191 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1192 trace("hpctime %u after %ums\n",
1193 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1195 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1196 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1197 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1198 last = pos;
1200 for(i=0; i < 9; i++) {
1201 Sleep(100);
1202 slept += 100;
1204 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1205 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1207 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1208 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1210 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1211 trace("hpctime %u pcpos %u\n",
1212 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1213 (ULONG)((pcpos-pcpos0)/10000));
1215 /* Use sum-pad to see whether position is ahead padding or not. */
1216 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1217 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1218 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1219 if (winetest_debug>1) {
1220 /* Padding does not lag behind by much */
1221 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1222 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1223 if (pad) /* not in case of underrun */
1224 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1225 "Position delta %ld not regular: %ld ms\n", (long)(pos-last), (long)((pos-last)*1000/freq));
1227 last = pos;
1229 hr = IAudioClient_GetStreamLatency(ac, &t1);
1230 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1231 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1233 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1234 data = NULL;
1235 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1236 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1237 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1238 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1239 if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1240 /* Only the first iteration should allow that large a buffer
1241 * as prefill was drained during the first 350+100ms sleep.
1242 * Afterwards, only 100ms of data should find room per iteration. */
1244 if(hr == S_OK) {
1245 trace("data at %p\n", data);
1246 } else {
1247 avail = gbsize - pad;
1248 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1249 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1250 trace("data at %p (small %u)\n", data, avail);
1252 ok(data != NULL, "NULL buffer returned\n");
1253 if(i % 3 && !winetest_interactive) {
1254 memset(data, 0, avail * pwfx->nBlockAlign);
1255 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1256 } else {
1257 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1258 wave_generate_tone(pwfx, data, avail));
1260 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1261 if(hr == S_OK) sum += avail;
1264 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1265 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1266 trace("position %u\n", (UINT)pos);
1268 Sleep(1000); /* 500ms buffer underrun past full buffer */
1270 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1271 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1273 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1274 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1275 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1277 if (share) {
1278 /* Following underrun, all samples were played */
1279 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1280 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1281 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1282 } else {
1283 /* Vista and w2k8 leave partial fragments behind */
1284 ok(pad == 0 /* w7, w2k8R2 */||
1285 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1286 /* expect at most 5 fragments (75ms) away */
1287 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1288 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1289 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1292 hr = IAudioClient_GetStreamLatency(ac, &t1);
1293 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1294 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1296 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1297 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1299 hr = IAudioClient_Stop(ac);
1300 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1302 CoTaskMemFree(pwfx);
1304 IAudioClock_Release(acl);
1305 IAudioRenderClient_Release(arc);
1306 IAudioClient_Release(ac);
1309 static void test_session(void)
1311 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1312 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1313 IMMDevice *cap_dev;
1314 GUID ses1_guid;
1315 AudioSessionState state;
1316 WAVEFORMATEX *pwfx;
1317 ULONG ref;
1318 HRESULT hr;
1320 hr = CoCreateGuid(&ses1_guid);
1321 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1323 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1324 NULL, (void**)&ses1_ac1);
1325 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1326 if (FAILED(hr)) return;
1328 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1329 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1331 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1332 0, 5000000, 0, pwfx, &ses1_guid);
1333 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1335 if(hr == S_OK){
1336 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1337 NULL, (void**)&ses1_ac2);
1338 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1340 if(hr != S_OK){
1341 skip("Unable to open the same device twice. Skipping session tests\n");
1343 ref = IAudioClient_Release(ses1_ac1);
1344 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1345 CoTaskMemFree(pwfx);
1346 return;
1349 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1350 0, 5000000, 0, pwfx, &ses1_guid);
1351 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1353 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1354 eMultimedia, &cap_dev);
1355 if(hr == S_OK){
1356 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1357 NULL, (void**)&cap_ac);
1358 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1359 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1360 IMMDevice_Release(cap_dev);
1362 if(hr == S_OK){
1363 WAVEFORMATEX *cap_pwfx;
1365 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1366 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1368 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1369 0, 5000000, 0, cap_pwfx, &ses1_guid);
1370 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1371 CoTaskMemFree(cap_pwfx);
1373 if(hr == S_OK){
1374 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1375 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1376 if(FAILED(hr))
1377 cap_ctl = NULL;
1378 }else
1379 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1381 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1382 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1384 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1385 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1387 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1388 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1389 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1390 ref = IAudioSessionControl2_Release(ses1_ctl2);
1391 ok(ref != 0, "AudioSessionControl was destroyed\n");
1393 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1394 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1396 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1397 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1399 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1400 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1401 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1403 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1404 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1405 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1407 if(cap_ctl){
1408 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1409 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1410 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1413 hr = IAudioClient_Start(ses1_ac1);
1414 ok(hr == S_OK, "Start failed: %08x\n", hr);
1416 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1417 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1418 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1420 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1421 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1422 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1424 if(cap_ctl){
1425 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1426 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1427 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1430 hr = IAudioClient_Stop(ses1_ac1);
1431 ok(hr == S_OK, "Start failed: %08x\n", hr);
1433 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1434 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1435 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1437 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1438 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1439 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1441 if(cap_ctl){
1442 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1443 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1444 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1446 hr = IAudioClient_Start(cap_ac);
1447 ok(hr == S_OK, "Start failed: %08x\n", hr);
1449 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1450 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1451 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1453 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1454 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1455 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1457 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1458 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1459 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1461 hr = IAudioClient_Stop(cap_ac);
1462 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1464 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1465 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1466 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1468 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1469 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1470 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1472 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1473 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1474 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1476 ref = IAudioSessionControl2_Release(cap_ctl);
1477 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1479 ref = IAudioClient_Release(cap_ac);
1480 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1483 ref = IAudioSessionControl2_Release(ses1_ctl);
1484 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1486 ref = IAudioClient_Release(ses1_ac1);
1487 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1489 ref = IAudioClient_Release(ses1_ac2);
1490 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1492 /* we've released all of our IAudioClient references, so check GetState */
1493 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1494 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1495 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1497 ref = IAudioSessionControl2_Release(ses1_ctl2);
1498 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1500 CoTaskMemFree(pwfx);
1503 static void test_streamvolume(void)
1505 IAudioClient *ac;
1506 IAudioStreamVolume *asv;
1507 WAVEFORMATEX *fmt;
1508 UINT32 chans, i;
1509 HRESULT hr;
1510 float vol, *vols;
1512 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1513 NULL, (void**)&ac);
1514 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1515 if(hr != S_OK)
1516 return;
1518 hr = IAudioClient_GetMixFormat(ac, &fmt);
1519 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1521 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1522 0, fmt, NULL);
1523 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1525 if(hr == S_OK){
1526 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1527 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1529 if(hr != S_OK){
1530 IAudioClient_Release(ac);
1531 CoTaskMemFree(fmt);
1532 return;
1535 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1536 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1538 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1539 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1540 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1542 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1543 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1545 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1546 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1548 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1549 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1551 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1552 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1553 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1555 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1556 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1558 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1559 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1561 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1562 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1564 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1565 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1567 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1568 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1569 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1571 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1572 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1574 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1575 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1577 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1578 ok(vols != NULL, "HeapAlloc failed\n");
1580 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1581 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1583 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1584 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1585 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1586 for(i = 1; i < fmt->nChannels; ++i)
1587 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1589 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1590 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1592 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1593 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1595 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1596 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1598 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1599 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1601 HeapFree(GetProcessHeap(), 0, vols);
1602 IAudioStreamVolume_Release(asv);
1603 IAudioClient_Release(ac);
1604 CoTaskMemFree(fmt);
1607 static void test_channelvolume(void)
1609 IAudioClient *ac;
1610 IChannelAudioVolume *acv;
1611 WAVEFORMATEX *fmt;
1612 UINT32 chans, i;
1613 HRESULT hr;
1614 float vol, *vols;
1616 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1617 NULL, (void**)&ac);
1618 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1619 if(hr != S_OK)
1620 return;
1622 hr = IAudioClient_GetMixFormat(ac, &fmt);
1623 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1625 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1626 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1627 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1629 if(hr == S_OK){
1630 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1631 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1633 if(hr != S_OK){
1634 IAudioClient_Release(ac);
1635 CoTaskMemFree(fmt);
1636 return;
1639 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1640 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1642 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1643 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1644 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1646 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1647 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1649 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1650 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1652 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1653 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1655 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1656 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1657 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1659 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1660 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1662 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1663 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1665 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1666 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1668 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1669 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1671 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1672 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1673 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1675 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1676 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1678 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1679 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1681 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1682 ok(vols != NULL, "HeapAlloc failed\n");
1684 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1685 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1687 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1688 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1689 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1690 for(i = 1; i < fmt->nChannels; ++i)
1691 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1693 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1694 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1696 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1697 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1699 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1700 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1702 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1703 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1705 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1706 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1708 HeapFree(GetProcessHeap(), 0, vols);
1709 IChannelAudioVolume_Release(acv);
1710 IAudioClient_Release(ac);
1711 CoTaskMemFree(fmt);
1714 static void test_simplevolume(void)
1716 IAudioClient *ac;
1717 ISimpleAudioVolume *sav;
1718 WAVEFORMATEX *fmt;
1719 HRESULT hr;
1720 float vol;
1721 BOOL mute;
1723 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1724 NULL, (void**)&ac);
1725 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1726 if(hr != S_OK)
1727 return;
1729 hr = IAudioClient_GetMixFormat(ac, &fmt);
1730 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1732 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1733 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1734 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1736 if(hr == S_OK){
1737 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1738 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1740 if(hr != S_OK){
1741 IAudioClient_Release(ac);
1742 CoTaskMemFree(fmt);
1743 return;
1746 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1747 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1749 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1750 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1751 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1753 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1754 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1756 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1757 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1759 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1760 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1762 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1763 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1764 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1766 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1767 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1769 mute = TRUE;
1770 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1771 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1772 ok(mute == FALSE, "Session is already muted\n");
1774 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1775 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1777 mute = FALSE;
1778 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1779 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1780 ok(mute == TRUE, "Session should have been muted\n");
1782 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1783 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1784 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1786 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1787 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1789 mute = FALSE;
1790 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1791 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1792 ok(mute == TRUE, "Session should have been muted\n");
1794 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1795 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1797 ISimpleAudioVolume_Release(sav);
1798 IAudioClient_Release(ac);
1799 CoTaskMemFree(fmt);
1802 static void test_volume_dependence(void)
1804 IAudioClient *ac, *ac2;
1805 ISimpleAudioVolume *sav;
1806 IChannelAudioVolume *cav;
1807 IAudioStreamVolume *asv;
1808 WAVEFORMATEX *fmt;
1809 HRESULT hr;
1810 float vol;
1811 GUID session;
1812 UINT32 nch;
1814 hr = CoCreateGuid(&session);
1815 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1817 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1818 NULL, (void**)&ac);
1819 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1820 if(hr != S_OK)
1821 return;
1823 hr = IAudioClient_GetMixFormat(ac, &fmt);
1824 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1826 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1827 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1828 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1830 if(hr == S_OK){
1831 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1832 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1834 if(hr != S_OK){
1835 IAudioClient_Release(ac);
1836 CoTaskMemFree(fmt);
1837 return;
1840 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1841 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
1843 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1844 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1846 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1847 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1849 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1850 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1852 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1853 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1855 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1856 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1857 ok(fabsf(vol - 0.2f) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1859 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1860 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1861 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1863 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1864 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1865 ok(fabsf(vol - 0.6f) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1867 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1868 NULL, (void**)&ac2);
1869 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1871 if(hr == S_OK){
1872 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1873 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1874 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1875 if(hr != S_OK)
1876 IAudioClient_Release(ac2);
1879 if(hr == S_OK){
1880 IChannelAudioVolume *cav2;
1881 IAudioStreamVolume *asv2;
1883 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1884 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1886 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1887 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1889 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1890 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1891 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1893 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1894 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1895 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1897 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1898 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1899 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1901 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1902 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1903 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1905 IAudioStreamVolume_Release(asv2);
1906 IChannelAudioVolume_Release(cav2);
1907 IAudioClient_Release(ac2);
1908 }else
1909 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1911 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1912 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1914 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1915 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1917 CoTaskMemFree(fmt);
1918 ISimpleAudioVolume_Release(sav);
1919 IChannelAudioVolume_Release(cav);
1920 IAudioStreamVolume_Release(asv);
1921 IAudioClient_Release(ac);
1924 static void test_session_creation(void)
1926 IMMDevice *cap_dev;
1927 IAudioClient *ac;
1928 IAudioSessionManager *sesm;
1929 ISimpleAudioVolume *sav;
1930 GUID session_guid;
1931 float vol;
1932 HRESULT hr;
1933 WAVEFORMATEX *fmt;
1935 CoCreateGuid(&session_guid);
1937 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1938 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1939 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1940 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1942 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1943 FALSE, &sav);
1944 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1946 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1947 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1949 /* Release completely to show session persistence */
1950 ISimpleAudioVolume_Release(sav);
1951 IAudioSessionManager_Release(sesm);
1953 /* test if we can create a capture audioclient in the session we just
1954 * created from a SessionManager derived from a render device */
1955 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1956 eMultimedia, &cap_dev);
1957 if(hr == S_OK){
1958 WAVEFORMATEX *cap_pwfx;
1959 IAudioClient *cap_ac;
1960 ISimpleAudioVolume *cap_sav;
1961 IAudioSessionManager *cap_sesm;
1963 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1964 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1965 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1966 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1968 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1969 FALSE, &cap_sav);
1970 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1972 vol = 0.5f;
1973 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1974 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1975 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
1977 ISimpleAudioVolume_Release(cap_sav);
1978 IAudioSessionManager_Release(cap_sesm);
1980 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1981 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1982 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1984 IMMDevice_Release(cap_dev);
1986 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1987 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1989 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1990 0, 5000000, 0, cap_pwfx, &session_guid);
1991 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1993 CoTaskMemFree(cap_pwfx);
1995 if(hr == S_OK){
1996 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
1997 (void**)&cap_sav);
1998 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2000 if(hr == S_OK){
2001 vol = 0.5f;
2002 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2003 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2004 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
2006 ISimpleAudioVolume_Release(cap_sav);
2009 IAudioClient_Release(cap_ac);
2012 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2013 NULL, (void**)&ac);
2014 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2015 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2016 if(hr != S_OK)
2017 return;
2019 hr = IAudioClient_GetMixFormat(ac, &fmt);
2020 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2022 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2023 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2024 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2026 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2027 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2028 if(hr == S_OK){
2029 vol = 0.5f;
2030 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2031 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2032 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2034 ISimpleAudioVolume_Release(sav);
2037 CoTaskMemFree(fmt);
2038 IAudioClient_Release(ac);
2041 static void test_worst_case(void)
2043 HANDLE event;
2044 HRESULT hr;
2045 IAudioClient *ac;
2046 IAudioRenderClient *arc;
2047 IAudioClock *acl;
2048 WAVEFORMATEX *pwfx;
2049 REFERENCE_TIME defp;
2050 UINT64 freq, pos, pcpos0, pcpos;
2051 BYTE *data;
2052 DWORD r;
2053 UINT32 pad, fragment, sum;
2054 int i,j;
2056 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2057 NULL, (void**)&ac);
2058 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2059 if(hr != S_OK)
2060 return;
2062 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2063 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2065 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2066 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2067 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2068 if(hr != S_OK)
2069 return;
2071 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2072 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2074 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2076 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2077 ok(event != NULL, "CreateEvent failed\n");
2079 hr = IAudioClient_SetEventHandle(ac, event);
2080 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2082 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2083 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2085 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2086 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2088 hr = IAudioClock_GetFrequency(acl, &freq);
2089 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2091 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2092 sum = 0;
2093 trace("Should play %ums continuous tone with fragment size %u.\n",
2094 (ULONG)(defp/100), fragment);
2096 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2097 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2099 /* XAudio2 prefills one period, play without it */
2100 if(winetest_debug>2){
2101 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2102 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2104 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2105 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2106 if(hr == S_OK)
2107 sum += fragment;
2110 hr = IAudioClient_Start(ac);
2111 ok(hr == S_OK, "Start failed: %08x\n", hr);
2113 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2114 r = WaitForSingleObject(event, 60 + defp / 10000);
2115 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2117 /* the app has nearly one period time to feed data */
2118 Sleep((i % 10) * defp / 120000);
2120 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2121 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2123 /* XAudio2 writes only when there's little data left */
2124 if(pad <= fragment){
2125 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2126 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2128 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2129 wave_generate_tone(pwfx, data, fragment));
2130 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2131 if(hr == S_OK)
2132 sum += fragment;
2136 hr = IAudioClient_Stop(ac);
2137 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2139 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2140 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2142 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2143 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2145 Sleep(100);
2147 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2148 sum, sum/fragment, fragment, pad,
2149 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2150 (ULONG)((pcpos-pcpos0)/10000));
2152 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2153 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2155 hr = IAudioClient_Reset(ac);
2156 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2158 Sleep(250);
2161 CoTaskMemFree(pwfx);
2162 IAudioClient_Release(ac);
2163 IAudioClock_Release(acl);
2164 IAudioRenderClient_Release(arc);
2167 static void test_marshal(void)
2169 IStream *pStream;
2170 IAudioClient *ac, *acDest;
2171 IAudioRenderClient *rc, *rcDest;
2172 WAVEFORMATEX *pwfx;
2173 HRESULT hr;
2175 /* IAudioRenderClient */
2176 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2177 NULL, (void**)&ac);
2178 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2179 if(hr != S_OK)
2180 return;
2182 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2183 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2185 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2186 0, pwfx, NULL);
2187 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2189 CoTaskMemFree(pwfx);
2191 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2192 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2193 if(hr != S_OK) {
2194 IAudioClient_Release(ac);
2195 return;
2198 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2199 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2201 /* marshal IAudioClient */
2203 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2204 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2206 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2207 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2208 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2209 if (hr == S_OK)
2210 IAudioClient_Release(acDest);
2212 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2213 /* marshal IAudioRenderClient */
2215 hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2216 ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2218 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2219 hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2220 ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2221 if (hr == S_OK)
2222 IAudioRenderClient_Release(rcDest);
2225 IStream_Release(pStream);
2227 IAudioClient_Release(ac);
2228 IAudioRenderClient_Release(rc);
2232 START_TEST(render)
2234 HRESULT hr;
2236 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2237 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2238 if (FAILED(hr))
2240 skip("mmdevapi not available: 0x%08x\n", hr);
2241 goto cleanup;
2244 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2245 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2246 if (hr != S_OK || !dev)
2248 if (hr == E_NOTFOUND)
2249 skip("No sound card available\n");
2250 else
2251 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2252 goto cleanup;
2255 test_audioclient();
2256 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2257 test_formats(AUDCLNT_SHAREMODE_SHARED);
2258 test_references();
2259 test_marshal();
2260 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2261 trace("Please redirect output to a file.\n");
2262 test_event();
2263 test_padding();
2264 test_clock(1);
2265 test_clock(0);
2266 test_session();
2267 test_streamvolume();
2268 test_channelvolume();
2269 test_simplevolume();
2270 test_volume_dependence();
2271 test_session_creation();
2272 test_worst_case();
2274 IMMDevice_Release(dev);
2276 cleanup:
2277 if (mme)
2278 IMMDeviceEnumerator_Release(mme);
2279 CoUninitialize();