TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / mmdevapi / tests / render.c
blob298c886c7a31f092ccdf21ade7bbecda32caef5a
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 /* 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(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
252 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
253 ok(hr == E_INVALIDARG ||
254 hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
256 /* A period != 0 is ignored and the call succeeds.
257 * Since we can only initialize successfully once, skip those tests.
259 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
260 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
262 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
263 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
264 if(hr == S_OK){
265 UINT32 num;
267 hr = IAudioClient_GetBufferSize(ac, &num);
268 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
269 if(hr == S_OK)
270 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
273 IAudioClient_Release(ac);
275 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
276 NULL, (void**)&ac);
277 ok(hr == S_OK, "Activation failed with %08x\n", hr);
279 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
280 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
281 WAVEFORMATEX *fmt2 = NULL;
283 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
285 fmtex->dwChannelMask = 0xffff;
287 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
288 ok(hr == S_OK ||
289 hr == AUDCLNT_E_UNSUPPORTED_FORMAT /* win10 */, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
291 IAudioClient_Release(ac);
293 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
294 NULL, (void**)&ac);
295 ok(hr == S_OK, "Activation failed with %08x\n", hr);
297 fmtex->dwChannelMask = 0;
299 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
300 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
301 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
302 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
304 CoTaskMemFree(fmt2);
306 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
307 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
309 IAudioClient_Release(ac);
311 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
312 NULL, (void**)&ac);
313 ok(hr == S_OK, "Activation failed with %08x\n", hr);
315 CoTaskMemFree(pwfx);
317 hr = IAudioClient_GetMixFormat(ac, &pwfx);
318 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
319 }else
320 skip("Skipping dwChannelMask tests\n");
322 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
323 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
324 if (hr != S_OK)
326 IAudioClient_Release(ac);
327 CoTaskMemFree(pwfx);
328 return;
331 hr = IAudioClient_GetStreamLatency(ac, NULL);
332 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
334 hr = IAudioClient_GetStreamLatency(ac, &t2);
335 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
336 trace("Returned latency: %u.%04u ms\n",
337 (UINT)(t2/10000), (UINT)(t2 % 10000));
338 ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000) ||
339 broken(t2 == 0) /* (!) win10 */,
340 "Latency < default period, delta %dus (%x%08x vs %x%08x)\n",
341 (LONG)((t2-t1)/10), (DWORD)(t2 >> 32), (DWORD)t2, (DWORD)(t1 >> 32), (DWORD)t1);
342 /* Native appears to add the engine period to the HW latency in shared mode */
343 if(t2 == 0)
344 win10 = TRUE;
346 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
347 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
349 hr = IAudioClient_SetEventHandle(ac, NULL);
350 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
352 hr = IAudioClient_SetEventHandle(ac, handle);
353 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
354 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
355 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
356 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
357 , "SetEventHandle returns %08x\n", hr);
359 hr = IAudioClient_Reset(ac);
360 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
362 hr = IAudioClient_Reset(ac);
363 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
365 hr = IAudioClient_Stop(ac);
366 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
368 hr = IAudioClient_Start(ac);
369 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
371 hr = IAudioClient_Start(ac);
372 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
374 IAudioClient_Release(ac);
376 CloseHandle(handle);
377 CoTaskMemFree(pwfx);
380 static void test_formats(AUDCLNT_SHAREMODE mode)
382 IAudioClient *ac;
383 HRESULT hr, hrs;
384 WAVEFORMATEX fmt, *pwfx, *pwfx2;
385 int i;
387 fmt.wFormatTag = WAVE_FORMAT_PCM;
388 fmt.cbSize = 0;
390 for(i = 0; i < NB_WIN_FORMATS; i++) {
391 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
392 NULL, (void**)&ac);
393 ok(hr == S_OK, "Activation failed with %08x\n", hr);
394 if(hr != S_OK)
395 continue;
397 hr = IAudioClient_GetMixFormat(ac, &pwfx);
398 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
400 fmt.nSamplesPerSec = win_formats[i][0];
401 fmt.wBitsPerSample = win_formats[i][1];
402 fmt.nChannels = win_formats[i][2];
403 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
404 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
406 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
407 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
408 hrs = hr;
409 /* Only shared mode suggests something ... GetMixFormat! */
410 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
411 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
412 /* 5:1 card exception when asked for 1 channel at mixer rate */
413 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
414 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
415 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
416 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
417 if (hr == S_OK)
418 trace("IsSupported(%s, %ux%2ux%u)\n",
419 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
420 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
422 /* Change GetMixFormat wBitsPerSample only => S_OK */
423 if (mode == AUDCLNT_SHAREMODE_SHARED
424 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
425 && fmt.nChannels == pwfx->nChannels)
426 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
428 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
429 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
430 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
431 if (pwfx2) {
432 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
433 pwfx2->nChannels == pwfx->nChannels &&
434 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
435 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
436 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
439 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
440 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
441 if ((hrs == S_OK) ^ (hr == S_OK))
442 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
443 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
444 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
445 if (mode == AUDCLNT_SHAREMODE_SHARED)
446 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
447 "Initialize(shared, %ux%2ux%u) returns %08x\n",
448 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
449 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
450 /* Unsupported format implies "create failed" and shadows "not allowed" */
451 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
452 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
453 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
454 else
455 /* On testbot 48000x16x1 claims support, but does not Initialize.
456 * Some cards Initialize 44100|48000x16x1 yet claim no support;
457 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
458 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
459 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == AUDCLNT_E_UNSUPPORTED_FORMAT ||
460 broken(hr == S_OK &&
461 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
462 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
463 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
464 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
466 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
467 * Release this ac and Activate a new one.
468 * A second call (with a known working format) would yield
469 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
470 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
471 * all subsequent calls until the audio engine service is restarted. */
473 CoTaskMemFree(pwfx2);
474 CoTaskMemFree(pwfx);
475 IAudioClient_Release(ac);
479 static void test_references(void)
481 IAudioClient *ac;
482 IAudioRenderClient *rc;
483 ISimpleAudioVolume *sav;
484 IAudioStreamVolume *asv;
485 IAudioClock *acl;
486 WAVEFORMATEX *pwfx;
487 HRESULT hr;
488 ULONG ref;
490 /* IAudioRenderClient */
491 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
492 NULL, (void**)&ac);
493 ok(hr == S_OK, "Activation failed with %08x\n", hr);
494 if(hr != S_OK)
495 return;
497 hr = IAudioClient_GetMixFormat(ac, &pwfx);
498 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
500 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
501 0, pwfx, NULL);
502 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
504 CoTaskMemFree(pwfx);
506 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
507 ok(hr == S_OK, "GetService failed: %08x\n", hr);
508 if(hr != S_OK) {
509 IAudioClient_Release(ac);
510 return;
513 IAudioRenderClient_AddRef(rc);
514 ref = IAudioRenderClient_Release(rc);
515 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
517 ref = IAudioClient_Release(ac);
518 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
520 ref = IAudioRenderClient_Release(rc);
521 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
523 /* ISimpleAudioVolume */
524 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
525 NULL, (void**)&ac);
526 ok(hr == S_OK, "Activation failed with %08x\n", hr);
527 if(hr != S_OK)
528 return;
530 hr = IAudioClient_GetMixFormat(ac, &pwfx);
531 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
533 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
534 0, pwfx, NULL);
535 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
537 CoTaskMemFree(pwfx);
539 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
540 ok(hr == S_OK, "GetService failed: %08x\n", hr);
542 ISimpleAudioVolume_AddRef(sav);
543 ref = ISimpleAudioVolume_Release(sav);
544 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
546 ref = IAudioClient_Release(ac);
547 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
549 ref = ISimpleAudioVolume_Release(sav);
550 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
552 /* IAudioClock */
553 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
554 NULL, (void**)&ac);
555 ok(hr == S_OK, "Activation failed with %08x\n", hr);
556 if(hr != S_OK)
557 return;
559 hr = IAudioClient_GetMixFormat(ac, &pwfx);
560 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
562 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
563 0, pwfx, NULL);
564 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
566 CoTaskMemFree(pwfx);
568 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
569 ok(hr == S_OK, "GetService failed: %08x\n", hr);
571 IAudioClock_AddRef(acl);
572 ref = IAudioClock_Release(acl);
573 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
575 ref = IAudioClient_Release(ac);
576 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
578 ref = IAudioClock_Release(acl);
579 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
581 /* IAudioStreamVolume */
582 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
583 NULL, (void**)&ac);
584 ok(hr == S_OK, "Activation failed with %08x\n", hr);
585 if(hr != S_OK)
586 return;
588 hr = IAudioClient_GetMixFormat(ac, &pwfx);
589 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
591 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
592 0, pwfx, NULL);
593 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
595 CoTaskMemFree(pwfx);
597 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
598 ok(hr == S_OK, "GetService failed: %08x\n", hr);
600 IAudioStreamVolume_AddRef(asv);
601 ref = IAudioStreamVolume_Release(asv);
602 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
604 ref = IAudioClient_Release(ac);
605 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
607 ref = IAudioStreamVolume_Release(asv);
608 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
611 static void test_event(void)
613 HANDLE event;
614 HRESULT hr;
615 DWORD r;
616 IAudioClient *ac;
617 WAVEFORMATEX *pwfx;
619 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
620 NULL, (void**)&ac);
621 ok(hr == S_OK, "Activation failed with %08x\n", hr);
622 if(hr != S_OK)
623 return;
625 hr = IAudioClient_GetMixFormat(ac, &pwfx);
626 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
628 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
629 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
630 0, pwfx, NULL);
631 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
633 CoTaskMemFree(pwfx);
635 event = CreateEventW(NULL, FALSE, FALSE, NULL);
636 ok(event != NULL, "CreateEvent failed\n");
638 hr = IAudioClient_Start(ac);
639 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET ||
640 hr == D3D11_ERROR_4E /* win10 */, "Start failed: %08x\n", hr);
642 hr = IAudioClient_SetEventHandle(ac, event);
643 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
645 hr = IAudioClient_SetEventHandle(ac, event);
646 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME) ||
647 hr == E_UNEXPECTED /* win10 */, "SetEventHandle returns %08x\n", hr);
649 r = WaitForSingleObject(event, 40);
650 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
652 hr = IAudioClient_Start(ac);
653 ok(hr == S_OK, "Start failed: %08x\n", hr);
655 r = WaitForSingleObject(event, 20);
656 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
658 hr = IAudioClient_Stop(ac);
659 ok(hr == S_OK, "Stop failed: %08x\n", hr);
661 ok(ResetEvent(event), "ResetEvent\n");
663 /* Still receiving events! */
664 r = WaitForSingleObject(event, 20);
665 ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
667 hr = IAudioClient_Reset(ac);
668 ok(hr == S_OK, "Reset failed: %08x\n", hr);
670 ok(ResetEvent(event), "ResetEvent\n");
672 r = WaitForSingleObject(event, 120);
673 ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
675 hr = IAudioClient_SetEventHandle(ac, NULL);
676 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
678 r = WaitForSingleObject(event, 70);
679 ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
681 /* test releasing a playing stream */
682 hr = IAudioClient_Start(ac);
683 ok(hr == S_OK, "Start failed: %08x\n", hr);
684 IAudioClient_Release(ac);
686 CloseHandle(event);
689 static void test_padding(void)
691 HRESULT hr;
692 IAudioClient *ac;
693 IAudioRenderClient *arc;
694 WAVEFORMATEX *pwfx;
695 REFERENCE_TIME minp, defp;
696 BYTE *buf, silence;
697 UINT32 psize, pad, written, i;
699 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
700 NULL, (void**)&ac);
701 ok(hr == S_OK, "Activation failed with %08x\n", hr);
702 if(hr != S_OK)
703 return;
705 hr = IAudioClient_GetMixFormat(ac, &pwfx);
706 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
707 if(hr != S_OK)
708 return;
710 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
711 0, 5000000, 0, pwfx, NULL);
712 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
713 if(hr != S_OK)
714 return;
716 if(pwfx->wBitsPerSample == 8)
717 silence = 128;
718 else
719 silence = 0;
721 /** GetDevicePeriod
722 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
723 * except when the HW/OS forces a particular alignment,
724 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
725 * 441 observed with Vista, 448 with w7 on the same HW! */
726 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
727 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
728 /* some wineXYZ.drv use 20ms, not seen on native */
729 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
730 "Expected 10ms default period: %u\n", (ULONG)defp);
731 ok(minp != 0, "Minimum period is 0\n");
732 ok(minp <= defp, "Mininum period is greater than default period\n");
734 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
735 ok(hr == S_OK, "GetService failed: %08x\n", hr);
737 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
739 written = 0;
740 hr = IAudioClient_GetCurrentPadding(ac, &pad);
741 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
742 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
744 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
745 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
746 ok(buf != NULL, "NULL buffer returned\n");
747 if(!win10){
748 /* win10 appears not to clear the buffer */
749 for(i = 0; i < psize * pwfx->nBlockAlign; ++i){
750 if(buf[i] != silence){
751 ok(0, "buffer has data in it already, i: %u, valu: %f\n", i, *((float*)buf));
752 break;
757 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
758 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
759 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
760 /* MSDN instead documents buf remains untouched */
762 hr = IAudioClient_Reset(ac);
763 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
765 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
766 AUDCLNT_BUFFERFLAGS_SILENT);
767 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
768 if(hr == S_OK) written += psize;
770 hr = IAudioClient_GetCurrentPadding(ac, &pad);
771 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
772 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
774 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
776 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
777 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
778 ok(buf != NULL, "NULL buffer returned\n");
780 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
781 AUDCLNT_BUFFERFLAGS_SILENT);
782 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
783 written += psize;
785 hr = IAudioClient_GetCurrentPadding(ac, &pad);
786 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
787 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
789 /* overfull buffer. requested 1/2s buffer size, so try
790 * to get a 1/2s buffer, which should fail */
791 psize = pwfx->nSamplesPerSec / 2;
792 buf = (void*)0xDEADF00D;
793 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
794 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
795 ok(buf == NULL, "NULL expected %p\n", buf);
797 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
798 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
800 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
802 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
803 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
804 ok(buf != NULL, "NULL buffer returned\n");
806 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
807 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
809 buf = (void*)0xDEADF00D;
810 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
811 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
812 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
813 /* MSDN instead documents buf remains untouched */
815 buf = (void*)0xDEADF00D;
816 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
817 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
818 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
820 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
821 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
823 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
824 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
825 ok(buf != NULL, "NULL buffer returned\n");
827 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
828 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
830 hr = IAudioClient_GetCurrentPadding(ac, &pad);
831 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
832 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
834 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
835 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
836 ok(buf != NULL, "NULL buffer returned\n");
838 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
839 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
840 /* todo_wine means Wine may overwrite memory */
841 if(hr == S_OK) written += psize+1;
843 /* Buffer still hold */
844 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
845 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
846 if(hr == S_OK) written += psize/2;
848 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
849 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
851 hr = IAudioClient_GetCurrentPadding(ac, &pad);
852 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
853 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
855 CoTaskMemFree(pwfx);
857 IAudioRenderClient_Release(arc);
858 IAudioClient_Release(ac);
861 static void test_clock(int share)
863 HRESULT hr;
864 IAudioClient *ac;
865 IAudioClock *acl;
866 IAudioRenderClient *arc;
867 UINT64 freq, pos, pcpos0, pcpos, last;
868 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
869 BYTE *data;
870 WAVEFORMATEX *pwfx;
871 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
872 REFERENCE_TIME minp, defp, t1, t2;
873 REFERENCE_TIME duration = 5000000, period = 150000;
874 int i;
876 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
878 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
879 NULL, (void**)&ac);
880 ok(hr == S_OK, "Activation failed with %08x\n", hr);
881 if(hr != S_OK)
882 return;
884 hr = IAudioClient_GetMixFormat(ac, &pwfx);
885 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
886 if(hr != S_OK)
887 return;
889 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
890 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
891 ok(minp <= period, "desired period %u too small for %u\n", (ULONG)period, (ULONG)minp);
893 if (share) {
894 trace("Testing shared mode\n");
895 /* period is ignored */
896 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
897 0, duration, period, pwfx, NULL);
898 period = defp;
899 } else {
900 pwfx->wFormatTag = WAVE_FORMAT_PCM;
901 pwfx->nChannels = 2;
902 pwfx->cbSize = 0;
903 pwfx->wBitsPerSample = 16; /* no floating point */
904 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
905 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
906 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
908 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
909 0, duration, period, pwfx, NULL);
911 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
912 if (hr != S_OK) {
913 CoTaskMemFree(pwfx);
914 IAudioClient_Release(ac);
915 if(hr == AUDCLNT_E_DEVICE_IN_USE)
916 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
917 return;
920 /** GetStreamLatency
921 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
922 * Exclusive mode: testbot returns 2x period + a little, but
923 * some HDA drivers return 1x period, some + a little. */
924 hr = IAudioClient_GetStreamLatency(ac, &t2);
925 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
926 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
927 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000) ||
928 broken(t2 == 0) /* win10 */,
929 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
931 /** GetBufferSize
932 * BufferSize must be rounded up, maximum 2s says MSDN.
933 * Both is wrong. Rounding may lead to size a little smaller than duration;
934 * duration > 2s is accepted in shared mode.
935 * Shared mode: round solely w.r.t. mixer rate,
936 * duration is no multiple of period.
937 * Exclusive mode: size appears as a multiple of some fragment that
938 * is either the rounded period or a fixed constant like 1024,
939 * whatever the driver implements. */
940 hr = IAudioClient_GetBufferSize(ac, &gbsize);
941 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
943 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
944 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
945 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
946 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
947 /* fragment size (= period in frames) is rounded up.
948 * BufferSize must be rounded up, maximum 2s says MSDN
949 * but it is rounded down modulo fragment ! */
950 if (share)
951 ok(gbsize == bufsize,
952 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
953 else
954 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
955 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
957 /* In shared mode, GetCurrentPadding decreases in multiples of
958 * fragment size (i.e. updated only at period ticks), whereas
959 * GetPosition appears to be reporting continuous positions.
960 * In exclusive mode, testbot behaves likewise, but native's Intel
961 * HDA driver shows no such deltas, GetCurrentPadding closely
962 * matches GetPosition, as in
963 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
965 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
966 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
968 hr = IAudioClock_GetFrequency(acl, &freq);
969 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
970 trace("Clock Frequency %u\n", (UINT)freq);
972 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
973 if (share)
974 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
975 "Clock Frequency %u\n", (UINT)freq);
976 else
977 ok(freq == pwfx->nSamplesPerSec,
978 "Clock Frequency %u\n", (UINT)freq);
980 hr = IAudioClock_GetPosition(acl, NULL, NULL);
981 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
983 pcpos0 = 0;
984 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
985 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
986 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
987 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
989 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
990 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
992 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
993 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
995 avail = gbsize;
996 data = NULL;
997 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
998 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
999 trace("data at %p\n", data);
1001 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1002 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1003 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1004 if(hr == S_OK) sum += avail;
1006 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1007 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1008 ok(pad == sum, "padding %u prior to start\n", pad);
1010 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1011 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1012 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
1014 hr = IAudioClient_Start(ac); /* #1 */
1015 ok(hr == S_OK, "Start failed: %08x\n", hr);
1017 Sleep(100);
1018 slept += 100;
1020 hr = IAudioClient_GetStreamLatency(ac, &t1);
1021 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1022 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1024 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1025 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1026 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1027 /* in rare cases is slept*1.1 not enough with dmix */
1028 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1029 last = pos;
1031 hr = IAudioClient_Stop(ac);
1032 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1034 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1035 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1036 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1037 last = pos;
1038 if(/*share &&*/ winetest_debug>1)
1039 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1041 hr = IAudioClient_Start(ac); /* #2 */
1042 ok(hr == S_OK, "Start failed: %08x\n", hr);
1044 Sleep(100);
1045 slept += 100;
1047 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1048 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1049 trace("padding %u past sleep #2\n", pad);
1051 /** IAudioClient_Stop
1052 * Exclusive mode: the audio engine appears to drop frames,
1053 * bumping GetPosition to a higher value than time allows, even
1054 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1055 * Shared mode: no drop observed (or too small to be visible).
1056 * GetPosition = sum Released - GetCurrentPadding
1057 * Bugs: Some USB headset system drained the whole buffer, leaving
1058 * padding 0 and bumping pos to sum minus 17 frames! */
1060 hr = IAudioClient_Stop(ac);
1061 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1063 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1064 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1066 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1067 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1068 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1069 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1070 /* Prove that Stop must not drop frames (in shared mode). */
1071 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1072 if (share && pad > 0 && winetest_debug>1)
1073 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1074 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1075 if(/*share &&*/ winetest_debug>1)
1076 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1077 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1078 last = pos;
1080 Sleep(100);
1082 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1083 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1084 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1086 /* Restart from 0 */
1087 hr = IAudioClient_Reset(ac);
1088 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1089 slept = sum = 0;
1091 hr = IAudioClient_Reset(ac);
1092 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1094 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1095 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1096 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1097 ok(pcpos > pcpos0, "pcpos should increase\n");
1099 avail = gbsize; /* implies GetCurrentPadding == 0 */
1100 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1101 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1102 trace("data at %p\n", data);
1104 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1105 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1106 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1107 if(hr == S_OK) sum += avail;
1109 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1110 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1111 ok(pad == sum, "padding %u prior to start\n", pad);
1113 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1114 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1115 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1116 last = pos;
1118 hr = IAudioClient_Start(ac); /* #3 */
1119 ok(hr == S_OK, "Start failed: %08x\n", hr);
1121 Sleep(100);
1122 slept += 100;
1124 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1125 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1126 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1127 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1128 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1129 if (winetest_debug>1)
1130 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1131 else
1132 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1133 last = pos;
1135 hr = IAudioClient_Reset(ac);
1136 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1138 hr = IAudioClient_Stop(ac);
1139 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1141 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1142 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1144 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1145 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1146 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1147 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1148 ok(pcpos > pcpos0, "pcpos should increase\n");
1149 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1150 if (pad > 0 && winetest_debug>1)
1151 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1152 if(winetest_debug>1)
1153 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1154 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1155 last = pos;
1157 /* Begin the big loop */
1158 hr = IAudioClient_Reset(ac);
1159 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1160 slept = last = sum = 0;
1161 pcpos0 = pcpos;
1163 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1165 hr = IAudioClient_Reset(ac);
1166 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1168 hr = IAudioClient_Start(ac);
1169 ok(hr == S_OK, "Start failed: %08x\n", hr);
1171 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1172 data = NULL;
1173 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1174 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1175 trace("data at %p for prefill %u\n", data, avail);
1177 if (winetest_debug>2) {
1178 hr = IAudioClient_Stop(ac);
1179 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1181 Sleep(20);
1182 slept += 20;
1184 hr = IAudioClient_Reset(ac);
1185 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1187 hr = IAudioClient_Start(ac);
1188 ok(hr == S_OK, "Start failed: %08x\n", hr);
1191 /* Despite passed time, data must still point to valid memory... */
1192 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1193 wave_generate_tone(pwfx, data, avail));
1194 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1195 if(hr == S_OK) sum += avail;
1197 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1198 * mixer may still have a little data. We believe an underrun will occur
1199 * when the mixer finds GCP smaller than a period size at the *end* of a
1200 * period cycle, i.e. shortly before calling SetEvent to signal the app
1201 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1202 * with no data written for over a period causes an underrun. */
1204 Sleep(350);
1205 slept += 350;
1206 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1207 trace("hpctime %u after %ums\n",
1208 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1210 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1211 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1212 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1213 last = pos;
1215 for(i=0; i < 9; i++) {
1216 Sleep(100);
1217 slept += 100;
1219 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1220 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1222 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1223 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1225 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1226 trace("hpctime %u pcpos %u\n",
1227 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1228 (ULONG)((pcpos-pcpos0)/10000));
1230 /* Use sum-pad to see whether position is ahead padding or not. */
1231 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1232 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1233 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1234 if (winetest_debug>1) {
1235 /* Padding does not lag behind by much */
1236 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1237 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1238 if (pad) /* not in case of underrun */
1239 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1240 "Position delta %ld not regular: %ld ms\n", (long)(pos-last), (long)((pos-last)*1000/freq));
1242 last = pos;
1244 hr = IAudioClient_GetStreamLatency(ac, &t1);
1245 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1246 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1248 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1249 data = NULL;
1250 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1251 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1252 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1253 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1254 if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1255 /* Only the first iteration should allow that large a buffer
1256 * as prefill was drained during the first 350+100ms sleep.
1257 * Afterwards, only 100ms of data should find room per iteration. */
1259 if(hr == S_OK) {
1260 trace("data at %p\n", data);
1261 } else {
1262 avail = gbsize - pad;
1263 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1264 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1265 trace("data at %p (small %u)\n", data, avail);
1267 ok(data != NULL, "NULL buffer returned\n");
1268 if(i % 3 && !winetest_interactive) {
1269 memset(data, 0, avail * pwfx->nBlockAlign);
1270 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1271 } else {
1272 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1273 wave_generate_tone(pwfx, data, avail));
1275 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1276 if(hr == S_OK) sum += avail;
1279 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1280 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1281 trace("position %u\n", (UINT)pos);
1283 Sleep(1000); /* 500ms buffer underrun past full buffer */
1285 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1286 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1288 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1289 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1290 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1292 if (share) {
1293 /* Following underrun, all samples were played */
1294 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1295 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1296 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1297 } else {
1298 /* Vista and w2k8 leave partial fragments behind */
1299 ok(pad == 0 /* w7, w2k8R2 */||
1300 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1301 /* expect at most 5 fragments (75ms) away */
1302 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1303 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1304 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1307 hr = IAudioClient_GetStreamLatency(ac, &t1);
1308 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1309 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1311 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1312 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1314 hr = IAudioClient_Stop(ac);
1315 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1317 CoTaskMemFree(pwfx);
1319 IAudioClock_Release(acl);
1320 IAudioRenderClient_Release(arc);
1321 IAudioClient_Release(ac);
1324 static void test_session(void)
1326 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1327 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1328 IMMDevice *cap_dev;
1329 GUID ses1_guid;
1330 AudioSessionState state;
1331 WAVEFORMATEX *pwfx;
1332 ULONG ref;
1333 HRESULT hr;
1335 hr = CoCreateGuid(&ses1_guid);
1336 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1338 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1339 NULL, (void**)&ses1_ac1);
1340 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1341 if (FAILED(hr)) return;
1343 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1344 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1346 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1347 0, 5000000, 0, pwfx, &ses1_guid);
1348 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1350 if(hr == S_OK){
1351 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1352 NULL, (void**)&ses1_ac2);
1353 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1355 if(hr != S_OK){
1356 skip("Unable to open the same device twice. Skipping session tests\n");
1358 ref = IAudioClient_Release(ses1_ac1);
1359 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1360 CoTaskMemFree(pwfx);
1361 return;
1364 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1365 0, 5000000, 0, pwfx, &ses1_guid);
1366 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1368 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1369 eMultimedia, &cap_dev);
1370 if(hr == S_OK){
1371 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1372 NULL, (void**)&cap_ac);
1373 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1374 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1375 IMMDevice_Release(cap_dev);
1377 if(hr == S_OK){
1378 WAVEFORMATEX *cap_pwfx;
1380 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1381 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1383 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1384 0, 5000000, 0, cap_pwfx, &ses1_guid);
1385 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1386 CoTaskMemFree(cap_pwfx);
1388 if(hr == S_OK){
1389 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1390 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1391 if(FAILED(hr))
1392 cap_ctl = NULL;
1393 }else
1394 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1396 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1397 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1399 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1400 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1402 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1403 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1404 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1405 ref = IAudioSessionControl2_Release(ses1_ctl2);
1406 ok(ref != 0, "AudioSessionControl was destroyed\n");
1408 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1409 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1411 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1412 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1414 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1415 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1416 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1418 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1419 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1420 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1422 if(cap_ctl){
1423 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1424 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1425 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1428 hr = IAudioClient_Start(ses1_ac1);
1429 ok(hr == S_OK, "Start failed: %08x\n", hr);
1431 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1432 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1433 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1435 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1436 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1437 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1439 if(cap_ctl){
1440 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1441 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1442 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1445 hr = IAudioClient_Stop(ses1_ac1);
1446 ok(hr == S_OK, "Start failed: %08x\n", hr);
1448 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1449 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1450 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1452 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1453 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1454 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1456 if(cap_ctl){
1457 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1458 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1459 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1461 hr = IAudioClient_Start(cap_ac);
1462 ok(hr == S_OK, "Start 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 == AudioSessionStateActive, "Got wrong state: %d\n", state);
1476 hr = IAudioClient_Stop(cap_ac);
1477 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1479 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1480 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1481 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1483 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1484 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1485 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1487 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1488 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1489 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1491 ref = IAudioSessionControl2_Release(cap_ctl);
1492 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1494 ref = IAudioClient_Release(cap_ac);
1495 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1498 ref = IAudioSessionControl2_Release(ses1_ctl);
1499 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1501 ref = IAudioClient_Release(ses1_ac1);
1502 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1504 ref = IAudioClient_Release(ses1_ac2);
1505 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1507 /* we've released all of our IAudioClient references, so check GetState */
1508 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1509 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1510 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1512 ref = IAudioSessionControl2_Release(ses1_ctl2);
1513 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1515 CoTaskMemFree(pwfx);
1518 static void test_streamvolume(void)
1520 IAudioClient *ac;
1521 IAudioStreamVolume *asv;
1522 WAVEFORMATEX *fmt;
1523 UINT32 chans, i;
1524 HRESULT hr;
1525 float vol, *vols;
1527 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1528 NULL, (void**)&ac);
1529 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1530 if(hr != S_OK)
1531 return;
1533 hr = IAudioClient_GetMixFormat(ac, &fmt);
1534 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1536 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1537 0, fmt, NULL);
1538 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1540 if(hr == S_OK){
1541 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1542 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1544 if(hr != S_OK){
1545 IAudioClient_Release(ac);
1546 CoTaskMemFree(fmt);
1547 return;
1550 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1551 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1553 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1554 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1555 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1557 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1558 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1560 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1561 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1563 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1564 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1566 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1567 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1568 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1570 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1571 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1573 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1574 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1576 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1577 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1579 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1580 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1582 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1583 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1584 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1586 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1587 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1589 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1590 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1592 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1593 ok(vols != NULL, "HeapAlloc failed\n");
1595 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1596 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1598 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1599 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1600 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1601 for(i = 1; i < fmt->nChannels; ++i)
1602 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1604 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1605 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1607 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1608 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1610 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1611 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1613 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1614 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1616 HeapFree(GetProcessHeap(), 0, vols);
1617 IAudioStreamVolume_Release(asv);
1618 IAudioClient_Release(ac);
1619 CoTaskMemFree(fmt);
1622 static void test_channelvolume(void)
1624 IAudioClient *ac;
1625 IChannelAudioVolume *acv;
1626 WAVEFORMATEX *fmt;
1627 UINT32 chans, i;
1628 HRESULT hr;
1629 float vol, *vols;
1631 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1632 NULL, (void**)&ac);
1633 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1634 if(hr != S_OK)
1635 return;
1637 hr = IAudioClient_GetMixFormat(ac, &fmt);
1638 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1640 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1641 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1642 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1644 if(hr == S_OK){
1645 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1646 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1648 if(hr != S_OK){
1649 IAudioClient_Release(ac);
1650 CoTaskMemFree(fmt);
1651 return;
1654 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1655 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1657 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1658 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1659 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1661 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1662 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1664 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1665 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1667 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1668 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1670 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1671 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1672 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1674 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1675 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1677 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1678 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1680 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1681 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1683 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1684 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1686 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1687 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1688 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1690 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1691 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1693 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1694 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1696 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1697 ok(vols != NULL, "HeapAlloc failed\n");
1699 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1700 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1702 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1703 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1704 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1705 for(i = 1; i < fmt->nChannels; ++i)
1706 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1708 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1709 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1711 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1712 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1714 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1715 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1717 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1718 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1720 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1721 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1723 HeapFree(GetProcessHeap(), 0, vols);
1724 IChannelAudioVolume_Release(acv);
1725 IAudioClient_Release(ac);
1726 CoTaskMemFree(fmt);
1729 static void test_simplevolume(void)
1731 IAudioClient *ac;
1732 ISimpleAudioVolume *sav;
1733 WAVEFORMATEX *fmt;
1734 HRESULT hr;
1735 float vol;
1736 BOOL mute;
1738 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1739 NULL, (void**)&ac);
1740 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1741 if(hr != S_OK)
1742 return;
1744 hr = IAudioClient_GetMixFormat(ac, &fmt);
1745 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1747 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1748 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1749 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1751 if(hr == S_OK){
1752 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1753 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1755 if(hr != S_OK){
1756 IAudioClient_Release(ac);
1757 CoTaskMemFree(fmt);
1758 return;
1761 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1762 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1764 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1765 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1766 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1768 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1769 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1771 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1772 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1774 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1775 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1777 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1778 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1779 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1781 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1782 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1784 mute = TRUE;
1785 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1786 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1787 ok(mute == FALSE, "Session is already muted\n");
1789 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1790 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1792 mute = FALSE;
1793 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1794 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1795 ok(mute == TRUE, "Session should have been muted\n");
1797 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1798 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1799 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1801 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1802 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1804 mute = FALSE;
1805 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1806 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1807 ok(mute == TRUE, "Session should have been muted\n");
1809 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1810 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1812 ISimpleAudioVolume_Release(sav);
1813 IAudioClient_Release(ac);
1814 CoTaskMemFree(fmt);
1817 static void test_volume_dependence(void)
1819 IAudioClient *ac, *ac2;
1820 ISimpleAudioVolume *sav;
1821 IChannelAudioVolume *cav;
1822 IAudioStreamVolume *asv;
1823 WAVEFORMATEX *fmt;
1824 HRESULT hr;
1825 float vol;
1826 GUID session;
1827 UINT32 nch;
1829 hr = CoCreateGuid(&session);
1830 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1832 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1833 NULL, (void**)&ac);
1834 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1835 if(hr != S_OK)
1836 return;
1838 hr = IAudioClient_GetMixFormat(ac, &fmt);
1839 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1841 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1842 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1843 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1845 if(hr == S_OK){
1846 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1847 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1849 if(hr != S_OK){
1850 IAudioClient_Release(ac);
1851 CoTaskMemFree(fmt);
1852 return;
1855 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1856 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
1858 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1859 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1861 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1862 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1864 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1865 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1867 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1868 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1870 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1871 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1872 ok(fabsf(vol - 0.2f) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1874 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1875 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1876 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1878 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1879 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1880 ok(fabsf(vol - 0.6f) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1882 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1883 NULL, (void**)&ac2);
1884 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1886 if(hr == S_OK){
1887 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1888 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1889 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1890 if(hr != S_OK)
1891 IAudioClient_Release(ac2);
1894 if(hr == S_OK){
1895 IChannelAudioVolume *cav2;
1896 IAudioStreamVolume *asv2;
1898 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1899 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1901 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1902 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1904 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1905 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1906 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1908 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1909 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1910 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1912 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1913 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1914 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1916 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1917 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1918 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1920 IAudioStreamVolume_Release(asv2);
1921 IChannelAudioVolume_Release(cav2);
1922 IAudioClient_Release(ac2);
1923 }else
1924 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1926 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1927 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1929 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1930 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1932 CoTaskMemFree(fmt);
1933 ISimpleAudioVolume_Release(sav);
1934 IChannelAudioVolume_Release(cav);
1935 IAudioStreamVolume_Release(asv);
1936 IAudioClient_Release(ac);
1939 static void test_session_creation(void)
1941 IMMDevice *cap_dev;
1942 IAudioClient *ac;
1943 IAudioSessionManager *sesm;
1944 ISimpleAudioVolume *sav;
1945 GUID session_guid;
1946 float vol;
1947 HRESULT hr;
1948 WAVEFORMATEX *fmt;
1950 CoCreateGuid(&session_guid);
1952 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1953 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1954 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1955 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1957 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1958 FALSE, &sav);
1959 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1961 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1962 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1964 /* Release completely to show session persistence */
1965 ISimpleAudioVolume_Release(sav);
1966 IAudioSessionManager_Release(sesm);
1968 /* test if we can create a capture audioclient in the session we just
1969 * created from a SessionManager derived from a render device */
1970 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1971 eMultimedia, &cap_dev);
1972 if(hr == S_OK){
1973 WAVEFORMATEX *cap_pwfx;
1974 IAudioClient *cap_ac;
1975 ISimpleAudioVolume *cap_sav;
1976 IAudioSessionManager *cap_sesm;
1978 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1979 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1980 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1981 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1983 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1984 FALSE, &cap_sav);
1985 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1987 vol = 0.5f;
1988 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1989 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1991 ISimpleAudioVolume_Release(cap_sav);
1992 IAudioSessionManager_Release(cap_sesm);
1994 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1995 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1996 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1998 IMMDevice_Release(cap_dev);
2000 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
2001 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2003 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
2004 0, 5000000, 0, cap_pwfx, &session_guid);
2005 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2007 CoTaskMemFree(cap_pwfx);
2009 if(hr == S_OK){
2010 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
2011 (void**)&cap_sav);
2012 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2014 if(hr == S_OK){
2015 vol = 0.5f;
2016 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2017 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2019 ISimpleAudioVolume_Release(cap_sav);
2022 IAudioClient_Release(cap_ac);
2025 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2026 NULL, (void**)&ac);
2027 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2028 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2029 if(hr != S_OK)
2030 return;
2032 hr = IAudioClient_GetMixFormat(ac, &fmt);
2033 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2035 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2036 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2037 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2039 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2040 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2041 if(hr == S_OK){
2042 vol = 0.5f;
2043 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2044 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2045 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2047 ISimpleAudioVolume_Release(sav);
2050 CoTaskMemFree(fmt);
2051 IAudioClient_Release(ac);
2054 static void test_worst_case(void)
2056 HANDLE event;
2057 HRESULT hr;
2058 IAudioClient *ac;
2059 IAudioRenderClient *arc;
2060 IAudioClock *acl;
2061 WAVEFORMATEX *pwfx;
2062 REFERENCE_TIME defp;
2063 UINT64 freq, pos, pcpos0, pcpos;
2064 BYTE *data;
2065 DWORD r;
2066 UINT32 pad, fragment, sum;
2067 int i,j;
2069 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2070 NULL, (void**)&ac);
2071 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2072 if(hr != S_OK)
2073 return;
2075 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2076 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2078 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2079 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2080 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2081 if(hr != S_OK)
2082 return;
2084 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2085 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2087 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2089 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2090 ok(event != NULL, "CreateEvent failed\n");
2092 hr = IAudioClient_SetEventHandle(ac, event);
2093 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2095 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2096 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2098 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2099 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2101 hr = IAudioClock_GetFrequency(acl, &freq);
2102 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2104 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2105 sum = 0;
2106 trace("Should play %ums continuous tone with fragment size %u.\n",
2107 (ULONG)(defp/100), fragment);
2109 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2110 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2112 /* XAudio2 prefills one period, play without it */
2113 if(winetest_debug>2){
2114 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2115 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2117 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2118 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2119 if(hr == S_OK)
2120 sum += fragment;
2123 hr = IAudioClient_Start(ac);
2124 ok(hr == S_OK, "Start failed: %08x\n", hr);
2126 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2127 r = WaitForSingleObject(event, 60 + defp / 10000);
2128 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2130 /* the app has nearly one period time to feed data */
2131 Sleep((i % 10) * defp / 120000);
2133 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2134 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2136 /* XAudio2 writes only when there's little data left */
2137 if(pad <= fragment){
2138 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2139 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2141 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2142 wave_generate_tone(pwfx, data, fragment));
2143 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2144 if(hr == S_OK)
2145 sum += fragment;
2149 hr = IAudioClient_Stop(ac);
2150 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2152 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2153 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2155 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2156 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2158 Sleep(100);
2160 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2161 sum, sum/fragment, fragment, pad,
2162 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2163 (ULONG)((pcpos-pcpos0)/10000));
2165 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2166 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2168 hr = IAudioClient_Reset(ac);
2169 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2171 Sleep(250);
2174 CoTaskMemFree(pwfx);
2175 IAudioClient_Release(ac);
2176 IAudioClock_Release(acl);
2177 IAudioRenderClient_Release(arc);
2180 static void test_marshal(void)
2182 IStream *pStream;
2183 IAudioClient *ac, *acDest;
2184 IAudioRenderClient *rc, *rcDest;
2185 WAVEFORMATEX *pwfx;
2186 HRESULT hr;
2188 /* IAudioRenderClient */
2189 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2190 NULL, (void**)&ac);
2191 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2192 if(hr != S_OK)
2193 return;
2195 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2196 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2198 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2199 0, pwfx, NULL);
2200 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2202 CoTaskMemFree(pwfx);
2204 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2205 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2206 if(hr != S_OK) {
2207 IAudioClient_Release(ac);
2208 return;
2211 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2212 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2214 /* marshal IAudioClient */
2216 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2217 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2219 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2220 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2221 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2222 if (hr == S_OK)
2223 IAudioClient_Release(acDest);
2225 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2226 /* marshal IAudioRenderClient */
2228 hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2229 ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2231 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2232 hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2233 ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2234 if (hr == S_OK)
2235 IAudioRenderClient_Release(rcDest);
2238 IStream_Release(pStream);
2240 IAudioClient_Release(ac);
2241 IAudioRenderClient_Release(rc);
2245 START_TEST(render)
2247 HRESULT hr;
2249 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2250 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2251 if (FAILED(hr))
2253 skip("mmdevapi not available: 0x%08x\n", hr);
2254 goto cleanup;
2257 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2258 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2259 if (hr != S_OK || !dev)
2261 if (hr == E_NOTFOUND)
2262 skip("No sound card available\n");
2263 else
2264 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2265 goto cleanup;
2268 test_audioclient();
2269 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2270 test_formats(AUDCLNT_SHAREMODE_SHARED);
2271 test_references();
2272 test_marshal();
2273 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2274 trace("Please redirect output to a file.\n");
2275 test_event();
2276 test_padding();
2277 test_clock(1);
2278 test_clock(0);
2279 test_session();
2280 test_streamvolume();
2281 test_channelvolume();
2282 test_simplevolume();
2283 test_volume_dependence();
2284 test_session_creation();
2285 test_worst_case();
2287 IMMDevice_Release(dev);
2289 cleanup:
2290 if (mme)
2291 IMMDeviceEnumerator_Release(mme);
2292 CoUninitialize();