makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / mmdevapi / tests / render.c
blob50a43229a0572fc4b446802c14343c77b0bf4c71
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"
42 #include "endpointvolume.h"
44 static const unsigned int win_formats[][4] = {
45 { 8000, 8, 1}, { 8000, 8, 2}, { 8000, 16, 1}, { 8000, 16, 2},
46 {11025, 8, 1}, {11025, 8, 2}, {11025, 16, 1}, {11025, 16, 2},
47 {12000, 8, 1}, {12000, 8, 2}, {12000, 16, 1}, {12000, 16, 2},
48 {16000, 8, 1}, {16000, 8, 2}, {16000, 16, 1}, {16000, 16, 2},
49 {22050, 8, 1}, {22050, 8, 2}, {22050, 16, 1}, {22050, 16, 2},
50 {44100, 8, 1}, {44100, 8, 2}, {44100, 16, 1}, {44100, 16, 2},
51 {48000, 8, 1}, {48000, 8, 2}, {48000, 16, 1}, {48000, 16, 2},
52 {96000, 8, 1}, {96000, 8, 2}, {96000, 16, 1}, {96000, 16, 2}
55 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
57 /* undocumented error code */
58 #define D3D11_ERROR_4E MAKE_HRESULT(SEVERITY_ERROR, FACILITY_DIRECT3D11, 0x4e)
60 static IMMDeviceEnumerator *mme = NULL;
61 static IMMDevice *dev = NULL;
62 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
63 static BOOL win10 = FALSE;
65 static const LARGE_INTEGER ullZero;
67 #define PI 3.14159265358979323846L
68 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
70 static double phase = 0.; /* normalized to unit, not 2*PI */
71 PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
72 DWORD cn, i;
73 double delta, y;
75 if(!winetest_interactive)
76 return AUDCLNT_BUFFERFLAGS_SILENT;
77 if(wfxe->Format.wBitsPerSample != ((wfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
78 IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) ? 8 * sizeof(float) : 16))
79 return AUDCLNT_BUFFERFLAGS_SILENT;
81 for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
82 delta += .5/wfxe->Format.nChannels, cn++){
83 for(i = 0; i < frames; i++){
84 y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
85 /* assume alignment is granted */
86 if(wfxe->Format.wBitsPerSample == 16)
87 ((short*)data)[cn+i*wfxe->Format.nChannels] = y * 32767.9;
88 else
89 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
92 phase += 440.* frames / wfxe->Format.nSamplesPerSec;
93 phase -= floor(phase);
94 return 0;
97 static void test_uninitialized(IAudioClient *ac)
99 HRESULT hr;
100 UINT32 num;
101 REFERENCE_TIME t1;
103 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
104 IUnknown *unk;
106 hr = IAudioClient_GetBufferSize(ac, &num);
107 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
109 hr = IAudioClient_GetStreamLatency(ac, &t1);
110 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
112 hr = IAudioClient_GetCurrentPadding(ac, &num);
113 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
115 hr = IAudioClient_Start(ac);
116 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
118 hr = IAudioClient_Stop(ac);
119 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
121 hr = IAudioClient_Reset(ac);
122 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
124 hr = IAudioClient_SetEventHandle(ac, handle);
125 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
127 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
128 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
130 CloseHandle(handle);
133 static void test_audioclient(void)
135 IAudioClient *ac;
136 IUnknown *unk;
137 HRESULT hr;
138 ULONG ref;
139 WAVEFORMATEX *pwfx, *pwfx2;
140 REFERENCE_TIME t1, t2;
141 HANDLE handle;
143 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
144 NULL, (void**)&ac);
145 ok(hr == S_OK, "Activation failed with %08x\n", hr);
146 if(hr != S_OK)
147 return;
149 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
151 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
152 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
154 unk = (void*)(LONG_PTR)0x12345678;
155 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
156 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
157 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
159 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
160 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
161 if (unk)
163 ref = IUnknown_Release(unk);
164 ok(ref == 1, "Released count is %u\n", ref);
167 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
168 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
169 if (unk)
171 ref = IUnknown_Release(unk);
172 ok(ref == 1, "Released count is %u\n", ref);
175 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
176 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
178 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
179 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
181 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
182 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
184 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
185 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
186 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
187 (UINT)(t1/10000), (UINT)(t1 % 10000),
188 (UINT)(t2/10000), (UINT)(t2 % 10000));
190 hr = IAudioClient_GetMixFormat(ac, NULL);
191 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
193 hr = IAudioClient_GetMixFormat(ac, &pwfx);
194 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
196 if (hr == S_OK)
198 trace("pwfx: %p\n", pwfx);
199 trace("Tag: %04x\n", pwfx->wFormatTag);
200 trace("bits: %u\n", pwfx->wBitsPerSample);
201 trace("chan: %u\n", pwfx->nChannels);
202 trace("rate: %u\n", pwfx->nSamplesPerSec);
203 trace("align: %u\n", pwfx->nBlockAlign);
204 trace("extra: %u\n", pwfx->cbSize);
205 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
206 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
208 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
209 trace("Res: %u\n", pwfxe->Samples.wReserved);
210 trace("Mask: %x\n", pwfxe->dwChannelMask);
211 trace("Alg: %s\n",
212 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
213 (IsEqualGUID(&pwfxe->SubFormat,
214 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
217 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
218 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
219 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
220 CoTaskMemFree(pwfx2);
222 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
223 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
225 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
226 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
228 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
229 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
230 "IsFormatSupported(Exclusive) call returns %08x\n", hr);
231 hexcl = hr;
233 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
234 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
235 ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
236 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
238 if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
239 hexcl = S_OK;
241 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
242 ok(hr == E_INVALIDARG/*w32*/ ||
243 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
244 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
247 test_uninitialized(ac);
249 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
250 ok(broken(hr == AUDCLNT_E_NOT_INITIALIZED) || /* <= win8 */
251 hr == E_INVALIDARG, "Initialize with invalid sharemode returns %08x\n", hr);
253 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
254 ok(hr == E_INVALIDARG ||
255 hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
257 /* A period != 0 is ignored and the call succeeds.
258 * Since we can only initialize successfully once, skip those tests.
260 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
261 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
263 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
264 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
265 if(hr == S_OK){
266 UINT32 num;
268 hr = IAudioClient_GetBufferSize(ac, &num);
269 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
270 if(hr == S_OK)
271 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
274 IAudioClient_Release(ac);
276 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
277 NULL, (void**)&ac);
278 ok(hr == S_OK, "Activation failed with %08x\n", hr);
280 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
281 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
282 WAVEFORMATEX *fmt2 = NULL;
284 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
286 fmtex->dwChannelMask = 0xffff;
288 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
289 ok(hr == S_OK ||
290 hr == AUDCLNT_E_UNSUPPORTED_FORMAT /* win10 */, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
292 IAudioClient_Release(ac);
294 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
295 NULL, (void**)&ac);
296 ok(hr == S_OK, "Activation failed with %08x\n", hr);
298 fmtex->dwChannelMask = 0;
300 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
301 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
302 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
303 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
305 CoTaskMemFree(fmt2);
307 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
308 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
310 IAudioClient_Release(ac);
312 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
313 NULL, (void**)&ac);
314 ok(hr == S_OK, "Activation failed with %08x\n", hr);
316 CoTaskMemFree(pwfx);
318 hr = IAudioClient_GetMixFormat(ac, &pwfx);
319 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
320 }else
321 skip("Skipping dwChannelMask tests\n");
323 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
324 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
325 if (hr != S_OK)
326 goto cleanup;
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 broken(t2 == 0) /* (!) win10 */,
337 "Latency < default period, delta %dus (%s vs %s)\n",
338 (LONG)((t2-t1)/10), wine_dbgstr_longlong(t2), wine_dbgstr_longlong(t1));
339 /* Native appears to add the engine period to the HW latency in shared mode */
340 if(t2 == 0)
341 win10 = TRUE;
343 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
344 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
346 hr = IAudioClient_SetEventHandle(ac, NULL);
347 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
349 hr = IAudioClient_SetEventHandle(ac, handle);
350 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
351 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
352 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
353 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
354 , "SetEventHandle returns %08x\n", hr);
356 hr = IAudioClient_Reset(ac);
357 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
359 hr = IAudioClient_Reset(ac);
360 ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
362 hr = IAudioClient_Stop(ac);
363 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
365 hr = IAudioClient_Start(ac);
366 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
368 hr = IAudioClient_Start(ac);
369 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
371 cleanup:
372 IAudioClient_Release(ac);
373 CloseHandle(handle);
374 CoTaskMemFree(pwfx);
377 static void test_formats(AUDCLNT_SHAREMODE mode)
379 IAudioClient *ac;
380 HRESULT hr, hrs;
381 WAVEFORMATEX fmt, *pwfx, *pwfx2;
382 int i;
384 fmt.wFormatTag = WAVE_FORMAT_PCM;
385 fmt.cbSize = 0;
387 for(i = 0; i < ARRAY_SIZE(win_formats); i++) {
388 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
389 NULL, (void**)&ac);
390 ok(hr == S_OK, "Activation failed with %08x\n", hr);
391 if(hr != S_OK)
392 continue;
394 hr = IAudioClient_GetMixFormat(ac, &pwfx);
395 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
397 fmt.nSamplesPerSec = win_formats[i][0];
398 fmt.wBitsPerSample = win_formats[i][1];
399 fmt.nChannels = win_formats[i][2];
400 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
401 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
403 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
404 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
405 hrs = hr;
406 /* Only shared mode suggests something ... GetMixFormat! */
407 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
408 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
409 /* 5:1 card exception when asked for 1 channel at mixer rate */
410 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
411 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
412 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
413 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
414 if (hr == S_OK)
415 trace("IsSupported(%s, %ux%2ux%u)\n",
416 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
417 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
419 /* Change GetMixFormat wBitsPerSample only => S_OK */
420 if (mode == AUDCLNT_SHAREMODE_SHARED
421 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
422 && fmt.nChannels == pwfx->nChannels)
423 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
425 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
426 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
427 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
428 if (pwfx2) {
429 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
430 pwfx2->nChannels == pwfx->nChannels &&
431 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
432 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
433 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
436 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
437 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
438 if ((hrs == S_OK) ^ (hr == S_OK))
439 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
440 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
441 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
442 if (mode == AUDCLNT_SHAREMODE_SHARED)
443 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
444 "Initialize(shared, %ux%2ux%u) returns %08x\n",
445 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
446 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
447 /* Unsupported format implies "create failed" and shadows "not allowed" */
448 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
449 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
450 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
451 else
452 /* On testbot 48000x16x1 claims support, but does not Initialize.
453 * Some cards Initialize 44100|48000x16x1 yet claim no support;
454 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
455 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
456 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == AUDCLNT_E_UNSUPPORTED_FORMAT ||
457 broken(hr == S_OK &&
458 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
459 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
460 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
461 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
463 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
464 * Release this ac and Activate a new one.
465 * A second call (with a known working format) would yield
466 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
467 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
468 * all subsequent calls until the audio engine service is restarted. */
470 CoTaskMemFree(pwfx2);
471 CoTaskMemFree(pwfx);
472 IAudioClient_Release(ac);
476 static void test_references(void)
478 IAudioClient *ac;
479 IAudioRenderClient *rc;
480 ISimpleAudioVolume *sav;
481 IAudioStreamVolume *asv;
482 IAudioClock *acl;
483 WAVEFORMATEX *pwfx;
484 HRESULT hr;
485 ULONG ref;
487 /* IAudioRenderClient */
488 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
489 NULL, (void**)&ac);
490 ok(hr == S_OK, "Activation failed with %08x\n", hr);
491 if(hr != S_OK)
492 return;
494 hr = IAudioClient_GetMixFormat(ac, &pwfx);
495 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
497 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
498 0, pwfx, NULL);
499 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
501 CoTaskMemFree(pwfx);
503 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
504 ok(hr == S_OK, "GetService failed: %08x\n", hr);
505 if(hr != S_OK) {
506 IAudioClient_Release(ac);
507 return;
510 IAudioRenderClient_AddRef(rc);
511 ref = IAudioRenderClient_Release(rc);
512 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
514 ref = IAudioClient_Release(ac);
515 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
517 ref = IAudioRenderClient_Release(rc);
518 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
520 /* ISimpleAudioVolume */
521 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
522 NULL, (void**)&ac);
523 ok(hr == S_OK, "Activation failed with %08x\n", hr);
524 if(hr != S_OK)
525 return;
527 hr = IAudioClient_GetMixFormat(ac, &pwfx);
528 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
530 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
531 0, pwfx, NULL);
532 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
534 CoTaskMemFree(pwfx);
536 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
537 ok(hr == S_OK, "GetService failed: %08x\n", hr);
539 ISimpleAudioVolume_AddRef(sav);
540 ref = ISimpleAudioVolume_Release(sav);
541 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
543 ref = IAudioClient_Release(ac);
544 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
546 ref = ISimpleAudioVolume_Release(sav);
547 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
549 /* IAudioClock */
550 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
551 NULL, (void**)&ac);
552 ok(hr == S_OK, "Activation failed with %08x\n", hr);
553 if(hr != S_OK)
554 return;
556 hr = IAudioClient_GetMixFormat(ac, &pwfx);
557 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
559 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
560 0, pwfx, NULL);
561 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
563 CoTaskMemFree(pwfx);
565 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
566 ok(hr == S_OK, "GetService failed: %08x\n", hr);
568 IAudioClock_AddRef(acl);
569 ref = IAudioClock_Release(acl);
570 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
572 ref = IAudioClient_Release(ac);
573 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
575 ref = IAudioClock_Release(acl);
576 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
578 /* IAudioStreamVolume */
579 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
580 NULL, (void**)&ac);
581 ok(hr == S_OK, "Activation failed with %08x\n", hr);
582 if(hr != S_OK)
583 return;
585 hr = IAudioClient_GetMixFormat(ac, &pwfx);
586 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
588 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
589 0, pwfx, NULL);
590 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
592 CoTaskMemFree(pwfx);
594 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
595 ok(hr == S_OK, "GetService failed: %08x\n", hr);
597 IAudioStreamVolume_AddRef(asv);
598 ref = IAudioStreamVolume_Release(asv);
599 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
601 ref = IAudioClient_Release(ac);
602 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
604 ref = IAudioStreamVolume_Release(asv);
605 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
608 static void test_event(void)
610 HANDLE event;
611 HRESULT hr;
612 DWORD r;
613 IAudioClient *ac;
614 WAVEFORMATEX *pwfx;
616 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
617 NULL, (void**)&ac);
618 ok(hr == S_OK, "Activation failed with %08x\n", hr);
619 if(hr != S_OK)
620 return;
622 hr = IAudioClient_GetMixFormat(ac, &pwfx);
623 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
625 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
626 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
627 0, pwfx, NULL);
628 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
630 CoTaskMemFree(pwfx);
632 event = CreateEventW(NULL, FALSE, FALSE, NULL);
633 ok(event != NULL, "CreateEvent failed\n");
635 hr = IAudioClient_Start(ac);
636 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET ||
637 hr == D3D11_ERROR_4E /* win10 */, "Start failed: %08x\n", hr);
639 hr = IAudioClient_SetEventHandle(ac, event);
640 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
642 hr = IAudioClient_SetEventHandle(ac, event);
643 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME) ||
644 hr == E_UNEXPECTED /* win10 */, "SetEventHandle returns %08x\n", hr);
646 r = WaitForSingleObject(event, 40);
647 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
649 hr = IAudioClient_Start(ac);
650 ok(hr == S_OK, "Start failed: %08x\n", hr);
652 r = WaitForSingleObject(event, 20);
653 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
655 hr = IAudioClient_Stop(ac);
656 ok(hr == S_OK, "Stop failed: %08x\n", hr);
658 ok(ResetEvent(event), "ResetEvent\n");
660 /* Still receiving events! */
661 r = WaitForSingleObject(event, 20);
662 ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
664 hr = IAudioClient_Reset(ac);
665 ok(hr == S_OK, "Reset failed: %08x\n", hr);
667 ok(ResetEvent(event), "ResetEvent\n");
669 r = WaitForSingleObject(event, 120);
670 ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
672 hr = IAudioClient_SetEventHandle(ac, NULL);
673 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
675 r = WaitForSingleObject(event, 70);
676 ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
678 /* test releasing a playing stream */
679 hr = IAudioClient_Start(ac);
680 ok(hr == S_OK, "Start failed: %08x\n", hr);
681 IAudioClient_Release(ac);
683 CloseHandle(event);
686 static void test_padding(void)
688 HRESULT hr;
689 IAudioClient *ac;
690 IAudioRenderClient *arc;
691 WAVEFORMATEX *pwfx;
692 REFERENCE_TIME minp, defp;
693 BYTE *buf, silence;
694 UINT32 psize, pad, written, i;
696 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
697 NULL, (void**)&ac);
698 ok(hr == S_OK, "Activation failed with %08x\n", hr);
699 if(hr != S_OK)
700 return;
702 hr = IAudioClient_GetMixFormat(ac, &pwfx);
703 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
704 if(hr != S_OK)
705 return;
707 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
708 0, 5000000, 0, pwfx, NULL);
709 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
710 if(hr != S_OK)
711 return;
713 if(pwfx->wBitsPerSample == 8)
714 silence = 128;
715 else
716 silence = 0;
718 /** GetDevicePeriod
719 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
720 * except when the HW/OS forces a particular alignment,
721 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
722 * 441 observed with Vista, 448 with w7 on the same HW! */
723 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
724 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
725 /* some wineXYZ.drv use 20ms, not seen on native */
726 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
727 "Expected 10ms default period: %u\n", (ULONG)defp);
728 ok(minp != 0, "Minimum period is 0\n");
729 ok(minp <= defp, "Minimum period is greater than default period\n");
731 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
732 ok(hr == S_OK, "GetService failed: %08x\n", hr);
734 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
736 written = 0;
737 hr = IAudioClient_GetCurrentPadding(ac, &pad);
738 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
739 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
741 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
742 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
743 ok(buf != NULL, "NULL buffer returned\n");
744 if(!win10){
745 /* win10 appears not to clear the buffer */
746 for(i = 0; i < psize * pwfx->nBlockAlign; ++i){
747 if(buf[i] != silence){
748 ok(0, "buffer has data in it already, i: %u, value: %f\n", i, *((float*)buf));
749 break;
754 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
755 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
756 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
757 /* MSDN instead documents buf remains untouched */
759 hr = IAudioClient_Reset(ac);
760 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
762 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
763 AUDCLNT_BUFFERFLAGS_SILENT);
764 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
765 if(hr == S_OK) written += psize;
767 hr = IAudioClient_GetCurrentPadding(ac, &pad);
768 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
769 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
771 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
773 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
774 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
775 ok(buf != NULL, "NULL buffer returned\n");
777 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
778 AUDCLNT_BUFFERFLAGS_SILENT);
779 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
780 written += psize;
782 hr = IAudioClient_GetCurrentPadding(ac, &pad);
783 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
784 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
786 /* overfull buffer. requested 1/2s buffer size, so try
787 * to get a 1/2s buffer, which should fail */
788 psize = pwfx->nSamplesPerSec / 2;
789 buf = (void*)0xDEADF00D;
790 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
791 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
792 ok(buf == NULL, "NULL expected %p\n", buf);
794 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
795 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
797 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
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 buf = (void*)0xDEADF00D;
807 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
808 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
809 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
810 /* MSDN instead documents buf remains untouched */
812 buf = (void*)0xDEADF00D;
813 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
814 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
815 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
817 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
818 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
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, 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 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
832 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
833 ok(buf != NULL, "NULL buffer returned\n");
835 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
836 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
837 /* todo_wine means Wine may overwrite memory */
838 if(hr == S_OK) written += psize+1;
840 /* Buffer still hold */
841 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
842 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
843 if(hr == S_OK) written += psize/2;
845 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
846 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
848 hr = IAudioClient_GetCurrentPadding(ac, &pad);
849 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
850 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
852 CoTaskMemFree(pwfx);
854 IAudioRenderClient_Release(arc);
855 IAudioClient_Release(ac);
858 static void test_clock(int share)
860 HRESULT hr;
861 IAudioClient *ac;
862 IAudioClock *acl;
863 IAudioRenderClient *arc;
864 UINT64 freq, pos, pcpos0, pcpos, last;
865 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
866 BYTE *data;
867 WAVEFORMATEX *pwfx;
868 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
869 REFERENCE_TIME minp, defp, t1, t2;
870 REFERENCE_TIME duration = 5000000, period = 150000;
871 int i;
873 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
875 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
876 NULL, (void**)&ac);
877 ok(hr == S_OK, "Activation failed with %08x\n", hr);
878 if(hr != S_OK)
879 return;
881 hr = IAudioClient_GetMixFormat(ac, &pwfx);
882 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
883 if(hr != S_OK)
884 return;
886 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
887 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
888 ok(minp <= period, "desired period %u too small for %u\n", (ULONG)period, (ULONG)minp);
890 if (share) {
891 trace("Testing shared mode\n");
892 /* period is ignored */
893 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
894 0, duration, period, pwfx, NULL);
895 period = defp;
896 } else {
897 pwfx->wFormatTag = WAVE_FORMAT_PCM;
898 pwfx->nChannels = 2;
899 pwfx->cbSize = 0;
900 pwfx->wBitsPerSample = 16; /* no floating point */
901 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
902 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
903 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
905 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
906 0, duration, period, pwfx, NULL);
908 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
909 if (hr != S_OK) {
910 CoTaskMemFree(pwfx);
911 IAudioClient_Release(ac);
912 if(hr == AUDCLNT_E_DEVICE_IN_USE)
913 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
914 return;
917 /** GetStreamLatency
918 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
919 * Exclusive mode: testbot returns 2x period + a little, but
920 * some HDA drivers return 1x period, some + a little. */
921 hr = IAudioClient_GetStreamLatency(ac, &t2);
922 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
923 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
924 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000) ||
925 broken(t2 == 0) /* win10 */,
926 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
928 /** GetBufferSize
929 * BufferSize must be rounded up, maximum 2s says MSDN.
930 * Both is wrong. Rounding may lead to size a little smaller than duration;
931 * duration > 2s is accepted in shared mode.
932 * Shared mode: round solely w.r.t. mixer rate,
933 * duration is no multiple of period.
934 * Exclusive mode: size appears as a multiple of some fragment that
935 * is either the rounded period or a fixed constant like 1024,
936 * whatever the driver implements. */
937 hr = IAudioClient_GetBufferSize(ac, &gbsize);
938 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
940 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
941 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
942 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
943 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
944 /* fragment size (= period in frames) is rounded up.
945 * BufferSize must be rounded up, maximum 2s says MSDN
946 * but it is rounded down modulo fragment ! */
947 if (share)
948 ok(gbsize == bufsize,
949 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
950 else
951 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
952 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
954 /* In shared mode, GetCurrentPadding decreases in multiples of
955 * fragment size (i.e. updated only at period ticks), whereas
956 * GetPosition appears to be reporting continuous positions.
957 * In exclusive mode, testbot behaves likewise, but native's Intel
958 * HDA driver shows no such deltas, GetCurrentPadding closely
959 * matches GetPosition, as in
960 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
962 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
963 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
965 hr = IAudioClock_GetFrequency(acl, &freq);
966 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
967 trace("Clock Frequency %u\n", (UINT)freq);
969 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
970 if (share)
971 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
972 "Clock Frequency %u\n", (UINT)freq);
973 else
974 ok(freq == pwfx->nSamplesPerSec,
975 "Clock Frequency %u\n", (UINT)freq);
977 hr = IAudioClock_GetPosition(acl, NULL, NULL);
978 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
980 pcpos0 = 0;
981 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
982 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
983 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
984 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
986 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
987 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
989 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
990 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
992 avail = gbsize;
993 data = NULL;
994 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
995 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
996 trace("data at %p\n", data);
998 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
999 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1000 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1001 if(hr == S_OK) sum += avail;
1003 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1004 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1005 ok(pad == sum, "padding %u prior to start\n", pad);
1007 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1008 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1009 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
1011 hr = IAudioClient_Start(ac); /* #1 */
1012 ok(hr == S_OK, "Start failed: %08x\n", hr);
1014 Sleep(100);
1015 slept += 100;
1017 hr = IAudioClient_GetStreamLatency(ac, &t1);
1018 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1019 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1021 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1022 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1023 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1024 /* in rare cases is slept*1.1 not enough with dmix */
1025 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1026 last = pos;
1028 hr = IAudioClient_Stop(ac);
1029 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1031 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1032 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1033 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1034 last = pos;
1035 if(/*share &&*/ winetest_debug>1)
1036 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1038 hr = IAudioClient_Start(ac); /* #2 */
1039 ok(hr == S_OK, "Start failed: %08x\n", hr);
1041 Sleep(100);
1042 slept += 100;
1044 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1045 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1046 trace("padding %u past sleep #2\n", pad);
1048 /** IAudioClient_Stop
1049 * Exclusive mode: the audio engine appears to drop frames,
1050 * bumping GetPosition to a higher value than time allows, even
1051 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1052 * Shared mode: no drop observed (or too small to be visible).
1053 * GetPosition = sum Released - GetCurrentPadding
1054 * Bugs: Some USB headset system drained the whole buffer, leaving
1055 * padding 0 and bumping pos to sum minus 17 frames! */
1057 hr = IAudioClient_Stop(ac);
1058 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1060 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1061 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1063 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1064 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1065 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1066 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1067 /* Prove that Stop must not drop frames (in shared mode). */
1068 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1069 if (share && pad > 0 && winetest_debug>1)
1070 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1071 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1072 if(/*share &&*/ winetest_debug>1)
1073 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1074 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1075 last = pos;
1077 Sleep(100);
1079 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1080 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1081 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1083 /* Restart from 0 */
1084 hr = IAudioClient_Reset(ac);
1085 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1086 slept = sum = 0;
1088 hr = IAudioClient_Reset(ac);
1089 ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
1091 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1092 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1093 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1094 ok(pcpos > pcpos0, "pcpos should increase\n");
1096 avail = gbsize; /* implies GetCurrentPadding == 0 */
1097 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1098 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1099 trace("data at %p\n", data);
1101 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1102 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1103 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1104 if(hr == S_OK) sum += avail;
1106 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1107 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1108 ok(pad == sum, "padding %u prior to start\n", pad);
1110 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1111 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1112 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1113 last = pos;
1115 hr = IAudioClient_Start(ac); /* #3 */
1116 ok(hr == S_OK, "Start failed: %08x\n", hr);
1118 Sleep(100);
1119 slept += 100;
1121 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1122 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1123 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1124 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1125 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1126 if (winetest_debug>1)
1127 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1128 else
1129 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1130 last = pos;
1132 hr = IAudioClient_Reset(ac);
1133 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1135 hr = IAudioClient_Stop(ac);
1136 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1138 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1139 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1141 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1142 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1143 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1144 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1145 ok(pcpos > pcpos0, "pcpos should increase\n");
1146 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1147 if (pad > 0 && winetest_debug>1)
1148 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1149 if(winetest_debug>1)
1150 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1151 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1152 last = pos;
1154 /* Begin the big loop */
1155 hr = IAudioClient_Reset(ac);
1156 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1157 slept = last = sum = 0;
1158 pcpos0 = pcpos;
1160 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1162 hr = IAudioClient_Reset(ac);
1163 ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
1165 hr = IAudioClient_Start(ac);
1166 ok(hr == S_OK, "Start failed: %08x\n", hr);
1168 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1169 data = NULL;
1170 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1171 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1172 trace("data at %p for prefill %u\n", data, avail);
1174 if (winetest_debug>2) {
1175 hr = IAudioClient_Stop(ac);
1176 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1178 Sleep(20);
1179 slept += 20;
1181 hr = IAudioClient_Reset(ac);
1182 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1184 hr = IAudioClient_Start(ac);
1185 ok(hr == S_OK, "Start failed: %08x\n", hr);
1188 /* Despite passed time, data must still point to valid memory... */
1189 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1190 wave_generate_tone(pwfx, data, avail));
1191 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1192 if(hr == S_OK) sum += avail;
1194 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1195 * mixer may still have a little data. We believe an underrun will occur
1196 * when the mixer finds GCP smaller than a period size at the *end* of a
1197 * period cycle, i.e. shortly before calling SetEvent to signal the app
1198 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1199 * with no data written for over a period causes an underrun. */
1201 Sleep(350);
1202 slept += 350;
1203 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1204 trace("hpctime %u after %ums\n",
1205 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1207 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1208 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1209 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1210 last = pos;
1212 for(i=0; i < 9; i++) {
1213 Sleep(100);
1214 slept += 100;
1216 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1217 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1219 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1220 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1222 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1223 trace("hpctime %u pcpos %u\n",
1224 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1225 (ULONG)((pcpos-pcpos0)/10000));
1227 /* Use sum-pad to see whether position is ahead padding or not. */
1228 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1229 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1230 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1231 if (winetest_debug>1) {
1232 /* Padding does not lag behind by much */
1233 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1234 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1235 if (pad) /* not in case of underrun */
1236 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1237 "Position delta %ld not regular: %ld ms\n", (long)(pos-last), (long)((pos-last)*1000/freq));
1239 last = pos;
1241 hr = IAudioClient_GetStreamLatency(ac, &t1);
1242 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1243 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1245 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1246 data = NULL;
1247 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1248 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1249 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1250 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1251 if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1252 /* Only the first iteration should allow that large a buffer
1253 * as prefill was drained during the first 350+100ms sleep.
1254 * Afterwards, only 100ms of data should find room per iteration. */
1256 if(hr == S_OK) {
1257 trace("data at %p\n", data);
1258 } else {
1259 avail = gbsize - pad;
1260 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1261 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1262 trace("data at %p (small %u)\n", data, avail);
1264 ok(data != NULL, "NULL buffer returned\n");
1265 if(i % 3 && !winetest_interactive) {
1266 memset(data, 0, avail * pwfx->nBlockAlign);
1267 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1268 } else {
1269 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1270 wave_generate_tone(pwfx, data, avail));
1272 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1273 if(hr == S_OK) sum += avail;
1276 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1277 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1278 trace("position %u\n", (UINT)pos);
1280 Sleep(1000); /* 500ms buffer underrun past full buffer */
1282 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1283 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1285 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1286 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1287 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1289 if (share) {
1290 /* Following underrun, all samples were played */
1291 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1292 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1293 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1294 } else {
1295 /* Vista and w2k8 leave partial fragments behind */
1296 ok(pad == 0 /* w7, w2k8R2 */||
1297 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1298 /* expect at most 5 fragments (75ms) away */
1299 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1300 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1301 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1304 hr = IAudioClient_GetStreamLatency(ac, &t1);
1305 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1306 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1308 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1309 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1311 hr = IAudioClient_Stop(ac);
1312 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1314 CoTaskMemFree(pwfx);
1316 IAudioClock_Release(acl);
1317 IAudioRenderClient_Release(arc);
1318 IAudioClient_Release(ac);
1321 static void test_session(void)
1323 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1324 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1325 IMMDevice *cap_dev;
1326 GUID ses1_guid;
1327 AudioSessionState state;
1328 WAVEFORMATEX *pwfx;
1329 ULONG ref;
1330 HRESULT hr;
1332 hr = CoCreateGuid(&ses1_guid);
1333 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1335 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1336 NULL, (void**)&ses1_ac1);
1337 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1338 if (FAILED(hr)) return;
1340 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1341 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1343 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1344 0, 5000000, 0, pwfx, &ses1_guid);
1345 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1347 if(hr == S_OK){
1348 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1349 NULL, (void**)&ses1_ac2);
1350 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1352 if(hr != S_OK){
1353 skip("Unable to open the same device twice. Skipping session tests\n");
1355 ref = IAudioClient_Release(ses1_ac1);
1356 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1357 CoTaskMemFree(pwfx);
1358 return;
1361 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1362 0, 5000000, 0, pwfx, &ses1_guid);
1363 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1365 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1366 eMultimedia, &cap_dev);
1367 if(hr == S_OK){
1368 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1369 NULL, (void**)&cap_ac);
1370 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1371 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1372 IMMDevice_Release(cap_dev);
1374 if(hr == S_OK){
1375 WAVEFORMATEX *cap_pwfx;
1377 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1378 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1380 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1381 0, 5000000, 0, cap_pwfx, &ses1_guid);
1382 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1383 CoTaskMemFree(cap_pwfx);
1385 if(hr == S_OK){
1386 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1387 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1388 if(FAILED(hr))
1389 cap_ctl = NULL;
1390 }else
1391 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1393 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1394 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1396 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1397 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1399 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1400 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1401 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1402 ref = IAudioSessionControl2_Release(ses1_ctl2);
1403 ok(ref != 0, "AudioSessionControl was destroyed\n");
1405 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1406 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1408 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1409 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1411 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1412 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1413 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1415 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1416 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1417 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1419 if(cap_ctl){
1420 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1421 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1422 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1425 hr = IAudioClient_Start(ses1_ac1);
1426 ok(hr == S_OK, "Start failed: %08x\n", hr);
1428 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1429 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1430 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1432 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1433 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1434 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1436 if(cap_ctl){
1437 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1438 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1439 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1442 hr = IAudioClient_Stop(ses1_ac1);
1443 ok(hr == S_OK, "Start failed: %08x\n", hr);
1445 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1446 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1447 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1449 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1450 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1451 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1453 if(cap_ctl){
1454 hr = IAudioSessionControl2_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 hr = IAudioClient_Start(cap_ac);
1459 ok(hr == S_OK, "Start failed: %08x\n", hr);
1461 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1462 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1463 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1465 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1466 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1467 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1469 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1470 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1471 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1473 hr = IAudioClient_Stop(cap_ac);
1474 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1476 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1477 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1478 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1480 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1481 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1482 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1484 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1485 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1486 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1488 ref = IAudioSessionControl2_Release(cap_ctl);
1489 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1491 ref = IAudioClient_Release(cap_ac);
1492 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1495 ref = IAudioSessionControl2_Release(ses1_ctl);
1496 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1498 ref = IAudioClient_Release(ses1_ac1);
1499 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1501 ref = IAudioClient_Release(ses1_ac2);
1502 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1504 /* we've released all of our IAudioClient references, so check GetState */
1505 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1506 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1507 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1509 ref = IAudioSessionControl2_Release(ses1_ctl2);
1510 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1512 CoTaskMemFree(pwfx);
1515 static void test_streamvolume(void)
1517 IAudioClient *ac;
1518 IAudioStreamVolume *asv;
1519 WAVEFORMATEX *fmt;
1520 UINT32 chans, i;
1521 HRESULT hr;
1522 float vol, *vols;
1524 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1525 NULL, (void**)&ac);
1526 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1527 if(hr != S_OK)
1528 return;
1530 hr = IAudioClient_GetMixFormat(ac, &fmt);
1531 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1533 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1534 0, fmt, NULL);
1535 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1537 if(hr == S_OK){
1538 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1539 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1541 if(hr != S_OK){
1542 IAudioClient_Release(ac);
1543 CoTaskMemFree(fmt);
1544 return;
1547 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1548 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1550 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1551 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1552 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1554 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1555 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1557 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1558 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1560 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1561 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1563 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1564 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1565 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1567 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1568 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1570 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1571 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1573 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1574 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1576 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1577 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1579 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1580 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1581 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1583 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1584 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1586 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1587 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1589 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1590 ok(vols != NULL, "HeapAlloc failed\n");
1592 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1593 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1595 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1596 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1597 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1598 for(i = 1; i < fmt->nChannels; ++i)
1599 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1601 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1602 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1604 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1605 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1607 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1608 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1610 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1611 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1613 HeapFree(GetProcessHeap(), 0, vols);
1614 IAudioStreamVolume_Release(asv);
1615 IAudioClient_Release(ac);
1616 CoTaskMemFree(fmt);
1619 static void test_channelvolume(void)
1621 IAudioClient *ac;
1622 IChannelAudioVolume *acv;
1623 WAVEFORMATEX *fmt;
1624 UINT32 chans, i;
1625 HRESULT hr;
1626 float vol, *vols;
1628 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1629 NULL, (void**)&ac);
1630 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1631 if(hr != S_OK)
1632 return;
1634 hr = IAudioClient_GetMixFormat(ac, &fmt);
1635 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1637 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1638 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1639 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1641 if(hr == S_OK){
1642 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1643 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1645 if(hr != S_OK){
1646 IAudioClient_Release(ac);
1647 CoTaskMemFree(fmt);
1648 return;
1651 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1652 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1654 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1655 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1656 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1658 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1659 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1661 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1662 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1664 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1665 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1667 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1668 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1669 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1671 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1672 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1674 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1675 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1677 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1678 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1680 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1681 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1683 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1684 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1685 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1687 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1688 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1690 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1691 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1693 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1694 ok(vols != NULL, "HeapAlloc failed\n");
1696 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1697 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1699 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1700 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1701 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1702 for(i = 1; i < fmt->nChannels; ++i)
1703 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1705 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1706 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1708 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1709 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1711 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1712 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1714 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1715 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1717 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1718 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1720 HeapFree(GetProcessHeap(), 0, vols);
1721 IChannelAudioVolume_Release(acv);
1722 IAudioClient_Release(ac);
1723 CoTaskMemFree(fmt);
1726 static void test_simplevolume(void)
1728 IAudioClient *ac;
1729 ISimpleAudioVolume *sav;
1730 WAVEFORMATEX *fmt;
1731 HRESULT hr;
1732 float vol;
1733 BOOL mute;
1735 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1736 NULL, (void**)&ac);
1737 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1738 if(hr != S_OK)
1739 return;
1741 hr = IAudioClient_GetMixFormat(ac, &fmt);
1742 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1744 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1745 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1746 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1748 if(hr == S_OK){
1749 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1750 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1752 if(hr != S_OK){
1753 IAudioClient_Release(ac);
1754 CoTaskMemFree(fmt);
1755 return;
1758 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1759 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1761 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1762 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1763 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1765 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1766 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1768 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1769 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1771 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1772 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1774 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1775 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1776 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1778 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1779 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1781 mute = TRUE;
1782 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1783 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1784 ok(mute == FALSE, "Session is already muted\n");
1786 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1787 ok(hr == S_OK, "SetMute 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_GetMasterVolume(sav, &vol);
1795 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1796 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1798 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1799 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1801 mute = FALSE;
1802 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1803 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1804 ok(mute == TRUE, "Session should have been muted\n");
1806 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1807 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1809 ISimpleAudioVolume_Release(sav);
1810 IAudioClient_Release(ac);
1811 CoTaskMemFree(fmt);
1814 static void test_volume_dependence(void)
1816 IAudioClient *ac, *ac2;
1817 ISimpleAudioVolume *sav;
1818 IChannelAudioVolume *cav;
1819 IAudioStreamVolume *asv;
1820 WAVEFORMATEX *fmt;
1821 HRESULT hr;
1822 float vol;
1823 GUID session;
1824 UINT32 nch;
1826 hr = CoCreateGuid(&session);
1827 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1829 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1830 NULL, (void**)&ac);
1831 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1832 if(hr != S_OK)
1833 return;
1835 hr = IAudioClient_GetMixFormat(ac, &fmt);
1836 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1838 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1839 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1840 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1842 if(hr == S_OK){
1843 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1844 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1846 if(hr != S_OK){
1847 IAudioClient_Release(ac);
1848 CoTaskMemFree(fmt);
1849 return;
1852 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1853 ok(hr == S_OK, "GetService (ChannelAudioVolume) failed: %08x\n", hr);
1855 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1856 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1858 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1859 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1861 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1862 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1864 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1865 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1867 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1868 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1869 ok(fabsf(vol - 0.2f) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1871 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1872 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1873 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1875 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1876 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1877 ok(fabsf(vol - 0.6f) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1879 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1880 NULL, (void**)&ac2);
1881 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1883 if(hr == S_OK){
1884 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1885 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1886 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1887 if(hr != S_OK)
1888 IAudioClient_Release(ac2);
1891 if(hr == S_OK){
1892 IChannelAudioVolume *cav2;
1893 IAudioStreamVolume *asv2;
1895 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1896 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1898 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1899 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1901 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1902 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1903 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1905 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1906 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1907 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1909 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1910 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1911 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1913 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1914 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1915 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1917 IAudioStreamVolume_Release(asv2);
1918 IChannelAudioVolume_Release(cav2);
1919 IAudioClient_Release(ac2);
1920 }else
1921 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1923 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1924 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1926 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1927 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1929 CoTaskMemFree(fmt);
1930 ISimpleAudioVolume_Release(sav);
1931 IChannelAudioVolume_Release(cav);
1932 IAudioStreamVolume_Release(asv);
1933 IAudioClient_Release(ac);
1936 static void test_session_creation(void)
1938 IMMDevice *cap_dev;
1939 IAudioClient *ac;
1940 IAudioSessionManager *sesm;
1941 ISimpleAudioVolume *sav;
1942 GUID session_guid;
1943 float vol;
1944 HRESULT hr;
1945 WAVEFORMATEX *fmt;
1947 CoCreateGuid(&session_guid);
1949 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1950 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1951 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1952 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1954 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1955 FALSE, &sav);
1956 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1958 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1959 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1961 /* Release completely to show session persistence */
1962 ISimpleAudioVolume_Release(sav);
1963 IAudioSessionManager_Release(sesm);
1965 /* test if we can create a capture audioclient in the session we just
1966 * created from a SessionManager derived from a render device */
1967 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1968 eMultimedia, &cap_dev);
1969 if(hr == S_OK){
1970 WAVEFORMATEX *cap_pwfx;
1971 IAudioClient *cap_ac;
1972 ISimpleAudioVolume *cap_sav;
1973 IAudioSessionManager *cap_sesm;
1975 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1976 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1977 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1978 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1980 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1981 FALSE, &cap_sav);
1982 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1984 vol = 0.5f;
1985 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1986 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1988 ISimpleAudioVolume_Release(cap_sav);
1989 IAudioSessionManager_Release(cap_sesm);
1991 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1992 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1993 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1995 IMMDevice_Release(cap_dev);
1997 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1998 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2000 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
2001 0, 5000000, 0, cap_pwfx, &session_guid);
2002 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2004 CoTaskMemFree(cap_pwfx);
2006 if(hr == S_OK){
2007 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
2008 (void**)&cap_sav);
2009 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2011 if(hr == S_OK){
2012 vol = 0.5f;
2013 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2014 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2016 ISimpleAudioVolume_Release(cap_sav);
2019 IAudioClient_Release(cap_ac);
2022 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2023 NULL, (void**)&ac);
2024 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2025 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2026 if(hr != S_OK)
2027 return;
2029 hr = IAudioClient_GetMixFormat(ac, &fmt);
2030 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2032 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2033 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2034 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2036 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2037 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2038 if(hr == S_OK){
2039 vol = 0.5f;
2040 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2041 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2042 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2044 ISimpleAudioVolume_Release(sav);
2047 CoTaskMemFree(fmt);
2048 IAudioClient_Release(ac);
2051 static void test_worst_case(void)
2053 HANDLE event;
2054 HRESULT hr;
2055 IAudioClient *ac;
2056 IAudioRenderClient *arc;
2057 IAudioClock *acl;
2058 WAVEFORMATEX *pwfx;
2059 REFERENCE_TIME defp;
2060 UINT64 freq, pos, pcpos0, pcpos;
2061 BYTE *data;
2062 DWORD r;
2063 UINT32 pad, fragment, sum;
2064 int i,j;
2066 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2067 NULL, (void**)&ac);
2068 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2069 if(hr != S_OK)
2070 return;
2072 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2073 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2075 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2076 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2077 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2078 if(hr != S_OK)
2079 return;
2081 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2082 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2084 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2086 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2087 ok(event != NULL, "CreateEvent failed\n");
2089 hr = IAudioClient_SetEventHandle(ac, event);
2090 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2092 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2093 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2095 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2096 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2098 hr = IAudioClock_GetFrequency(acl, &freq);
2099 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2101 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2102 sum = 0;
2103 trace("Should play %ums continuous tone with fragment size %u.\n",
2104 (ULONG)(defp/100), fragment);
2106 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2107 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2109 /* XAudio2 prefills one period, play without it */
2110 if(winetest_debug>2){
2111 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2112 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2114 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2115 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2116 if(hr == S_OK)
2117 sum += fragment;
2120 hr = IAudioClient_Start(ac);
2121 ok(hr == S_OK, "Start failed: %08x\n", hr);
2123 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2124 r = WaitForSingleObject(event, 60 + defp / 10000);
2125 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2127 /* the app has nearly one period time to feed data */
2128 Sleep((i % 10) * defp / 120000);
2130 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2131 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2133 /* XAudio2 writes only when there's little data left */
2134 if(pad <= fragment){
2135 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2136 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2138 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2139 wave_generate_tone(pwfx, data, fragment));
2140 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2141 if(hr == S_OK)
2142 sum += fragment;
2146 hr = IAudioClient_Stop(ac);
2147 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2149 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2150 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2152 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2153 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2155 Sleep(100);
2157 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2158 sum, sum/fragment, fragment, pad,
2159 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2160 (ULONG)((pcpos-pcpos0)/10000));
2162 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2163 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2165 hr = IAudioClient_Reset(ac);
2166 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2168 Sleep(250);
2171 CoTaskMemFree(pwfx);
2172 IAudioClient_Release(ac);
2173 IAudioClock_Release(acl);
2174 IAudioRenderClient_Release(arc);
2177 static void test_marshal(void)
2179 IStream *pStream;
2180 IAudioClient *ac, *acDest;
2181 IAudioRenderClient *rc, *rcDest;
2182 WAVEFORMATEX *pwfx;
2183 HRESULT hr;
2185 /* IAudioRenderClient */
2186 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2187 NULL, (void**)&ac);
2188 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2189 if(hr != S_OK)
2190 return;
2192 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2193 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2195 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2196 0, pwfx, NULL);
2197 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2199 CoTaskMemFree(pwfx);
2201 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2202 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2203 if(hr != S_OK) {
2204 IAudioClient_Release(ac);
2205 return;
2208 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2209 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2211 /* marshal IAudioClient */
2213 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2214 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2216 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2217 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2218 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2219 if (hr == S_OK)
2220 IAudioClient_Release(acDest);
2222 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2223 /* marshal IAudioRenderClient */
2225 hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2226 ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2228 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2229 hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2230 ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2231 if (hr == S_OK)
2232 IAudioRenderClient_Release(rcDest);
2235 IStream_Release(pStream);
2237 IAudioClient_Release(ac);
2238 IAudioRenderClient_Release(rc);
2242 static void test_endpointvolume(void)
2244 HRESULT hr;
2245 IAudioEndpointVolume *aev;
2246 float mindb, maxdb, increment, volume;
2247 BOOL mute;
2249 hr = IMMDevice_Activate(dev, &IID_IAudioEndpointVolume,
2250 CLSCTX_INPROC_SERVER, NULL, (void**)&aev);
2251 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2252 if(hr != S_OK)
2253 return;
2255 hr = IAudioEndpointVolume_GetVolumeRange(aev, &mindb, NULL, NULL);
2256 ok(hr == E_POINTER, "GetVolumeRange should have failed with E_POINTER: 0x%08x\n", hr);
2258 hr = IAudioEndpointVolume_GetVolumeRange(aev, &mindb, &maxdb, &increment);
2259 ok(hr == S_OK, "GetVolumeRange failed: 0x%08x\n", hr);
2260 trace("got range: [%f,%f]/%f\n", mindb, maxdb, increment);
2262 hr = IAudioEndpointVolume_SetMasterVolumeLevel(aev, mindb - increment, NULL);
2263 ok(hr == E_INVALIDARG, "SetMasterVolumeLevel failed: 0x%08x\n", hr);
2265 hr = IAudioEndpointVolume_GetMasterVolumeLevel(aev, &volume);
2266 ok(hr == S_OK, "GetMasterVolumeLevel failed: 0x%08x\n", hr);
2268 hr = IAudioEndpointVolume_SetMasterVolumeLevel(aev, volume, NULL);
2269 ok(hr == S_OK, "SetMasterVolumeLevel failed: 0x%08x\n", hr);
2271 hr = IAudioEndpointVolume_GetMute(aev, &mute);
2272 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
2274 hr = IAudioEndpointVolume_SetMute(aev, mute, NULL);
2275 ok(hr == S_OK || hr == S_FALSE, "SetMute failed: %08x\n", hr);
2277 IAudioEndpointVolume_Release(aev);
2280 START_TEST(render)
2282 HRESULT hr;
2284 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2285 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2286 if (FAILED(hr))
2288 skip("mmdevapi not available: 0x%08x\n", hr);
2289 goto cleanup;
2292 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2293 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2294 if (hr != S_OK || !dev)
2296 if (hr == E_NOTFOUND)
2297 skip("No sound card available\n");
2298 else
2299 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2300 goto cleanup;
2303 test_audioclient();
2304 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2305 test_formats(AUDCLNT_SHAREMODE_SHARED);
2306 test_references();
2307 test_marshal();
2308 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2309 trace("Please redirect output to a file.\n");
2310 test_event();
2311 test_padding();
2312 test_clock(1);
2313 test_clock(0);
2314 test_session();
2315 test_streamvolume();
2316 test_channelvolume();
2317 test_simplevolume();
2318 test_volume_dependence();
2319 test_session_creation();
2320 test_worst_case();
2321 test_endpointvolume();
2323 IMMDevice_Release(dev);
2325 cleanup:
2326 if (mme)
2327 IMMDeviceEnumerator_Release(mme);
2328 CoUninitialize();