Assorted spelling fixes.
[wine/multimedia.git] / dlls / mmdevapi / tests / render.c
blob4117cb31ef32cfea0daea610d55ab52eac8b1d51
1 /*
2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
3 * 2011-2012 Jörg Höhle
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* This test is for audio playback specific mechanisms
21 * Tests:
22 * - IAudioClient with eRender and IAudioRenderClient
25 #include <math.h>
26 #include <stdio.h>
28 #include "wine/test.h"
30 #define COBJMACROS
32 #ifdef STANDALONE
33 #include "initguid.h"
34 #endif
36 #include "unknwn.h"
37 #include "uuids.h"
38 #include "mmdeviceapi.h"
39 #include "mmsystem.h"
40 #include "audioclient.h"
41 #include "audiopolicy.h"
43 static const unsigned int win_formats[][4] = {
44 { 8000, 8, 1}, { 8000, 8, 2}, { 8000, 16, 1}, { 8000, 16, 2},
45 {11025, 8, 1}, {11025, 8, 2}, {11025, 16, 1}, {11025, 16, 2},
46 {12000, 8, 1}, {12000, 8, 2}, {12000, 16, 1}, {12000, 16, 2},
47 {16000, 8, 1}, {16000, 8, 2}, {16000, 16, 1}, {16000, 16, 2},
48 {22050, 8, 1}, {22050, 8, 2}, {22050, 16, 1}, {22050, 16, 2},
49 {44100, 8, 1}, {44100, 8, 2}, {44100, 16, 1}, {44100, 16, 2},
50 {48000, 8, 1}, {48000, 8, 2}, {48000, 16, 1}, {48000, 16, 2},
51 {96000, 8, 1}, {96000, 8, 2}, {96000, 16, 1}, {96000, 16, 2}
53 #define NB_WIN_FORMATS (sizeof(win_formats)/sizeof(*win_formats))
55 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
57 static IMMDeviceEnumerator *mme = NULL;
58 static IMMDevice *dev = NULL;
59 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
61 static const LARGE_INTEGER ullZero;
63 static inline const char *dbgstr_guid( const GUID *id )
65 static char ret[256];
66 sprintf(ret, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
67 id->Data1, id->Data2, id->Data3,
68 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
69 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
70 return ret;
73 #define PI 3.14159265358979323846L
74 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
76 static double phase = 0.; /* normalized to unit, not 2*PI */
77 PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
78 DWORD cn, i;
79 double delta, y;
81 if(!winetest_interactive)
82 return AUDCLNT_BUFFERFLAGS_SILENT;
83 if(wfxe->Format.wBitsPerSample != ((wfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
84 IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) ? 8 * sizeof(float) : 16))
85 return AUDCLNT_BUFFERFLAGS_SILENT;
87 for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
88 delta += .5/wfxe->Format.nChannels, cn++){
89 for(i = 0; i < frames; i++){
90 y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
91 /* assume alignment is granted */
92 if(wfxe->Format.wBitsPerSample == 16)
93 ((short*)data)[cn+i*wfxe->Format.nChannels] = y * 32767.9;
94 else
95 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
98 phase += 440.* frames / wfxe->Format.nSamplesPerSec;
99 phase -= floor(phase);
100 return 0;
103 static void test_uninitialized(IAudioClient *ac)
105 HRESULT hr;
106 UINT32 num;
107 REFERENCE_TIME t1;
109 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
110 IUnknown *unk;
112 hr = IAudioClient_GetBufferSize(ac, &num);
113 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
115 hr = IAudioClient_GetStreamLatency(ac, &t1);
116 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
118 hr = IAudioClient_GetCurrentPadding(ac, &num);
119 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
121 hr = IAudioClient_Start(ac);
122 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
124 hr = IAudioClient_Stop(ac);
125 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
127 hr = IAudioClient_Reset(ac);
128 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
130 hr = IAudioClient_SetEventHandle(ac, handle);
131 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
133 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
134 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
136 CloseHandle(handle);
139 static void test_audioclient(void)
141 IAudioClient *ac;
142 IUnknown *unk;
143 HRESULT hr;
144 ULONG ref;
145 WAVEFORMATEX *pwfx, *pwfx2;
146 REFERENCE_TIME t1, t2;
147 HANDLE handle;
149 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
150 NULL, (void**)&ac);
151 ok(hr == S_OK, "Activation failed with %08x\n", hr);
152 if(hr != S_OK)
153 return;
155 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
157 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
158 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
160 unk = (void*)(LONG_PTR)0x12345678;
161 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
162 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
163 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
165 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
166 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
167 if (unk)
169 ref = IUnknown_Release(unk);
170 ok(ref == 1, "Released count is %u\n", ref);
173 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
174 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
175 if (unk)
177 ref = IUnknown_Release(unk);
178 ok(ref == 1, "Released count is %u\n", ref);
181 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
182 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
184 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
185 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
187 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
188 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
190 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
191 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
192 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
193 (UINT)(t1/10000), (UINT)(t1 % 10000),
194 (UINT)(t2/10000), (UINT)(t2 % 10000));
196 hr = IAudioClient_GetMixFormat(ac, NULL);
197 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
199 hr = IAudioClient_GetMixFormat(ac, &pwfx);
200 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
202 if (hr == S_OK)
204 trace("pwfx: %p\n", pwfx);
205 trace("Tag: %04x\n", pwfx->wFormatTag);
206 trace("bits: %u\n", pwfx->wBitsPerSample);
207 trace("chan: %u\n", pwfx->nChannels);
208 trace("rate: %u\n", pwfx->nSamplesPerSec);
209 trace("align: %u\n", pwfx->nBlockAlign);
210 trace("extra: %u\n", pwfx->cbSize);
211 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
212 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
214 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
215 trace("Res: %u\n", pwfxe->Samples.wReserved);
216 trace("Mask: %x\n", pwfxe->dwChannelMask);
217 trace("Alg: %s\n",
218 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
219 (IsEqualGUID(&pwfxe->SubFormat,
220 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
223 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
224 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
225 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
226 CoTaskMemFree(pwfx2);
228 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
229 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
231 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
232 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
234 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
235 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
236 "IsFormatSupported(Exclusive) call returns %08x\n", hr);
237 hexcl = hr;
239 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
240 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
241 ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
242 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
244 if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
245 hexcl = S_OK;
247 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
248 ok(hr == E_INVALIDARG/*w32*/ ||
249 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
250 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
253 test_uninitialized(ac);
255 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
256 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
258 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
259 ok(hr == E_INVALIDARG ||
260 hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
262 /* A period != 0 is ignored and the call succeeds.
263 * Since we can only initialize successfully once, skip those tests.
265 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
266 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
268 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
269 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
270 if(hr == S_OK){
271 UINT32 num;
273 hr = IAudioClient_GetBufferSize(ac, &num);
274 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
275 if(hr == S_OK)
276 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
279 IAudioClient_Release(ac);
281 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
282 NULL, (void**)&ac);
283 ok(hr == S_OK, "Activation failed with %08x\n", hr);
285 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
286 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
287 WAVEFORMATEX *fmt2 = NULL;
289 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
291 fmtex->dwChannelMask = 0xffff;
293 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
294 ok(hr == S_OK, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
296 IAudioClient_Release(ac);
298 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
299 NULL, (void**)&ac);
300 ok(hr == S_OK, "Activation failed with %08x\n", hr);
302 fmtex->dwChannelMask = 0;
304 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
305 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
306 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
307 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
309 CoTaskMemFree(fmt2);
311 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
312 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
314 IAudioClient_Release(ac);
316 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
317 NULL, (void**)&ac);
318 ok(hr == S_OK, "Activation failed with %08x\n", hr);
320 CoTaskMemFree(pwfx);
322 hr = IAudioClient_GetMixFormat(ac, &pwfx);
323 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
324 }else
325 skip("Skipping dwChannelMask tests\n");
327 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
328 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
329 if (hr != S_OK)
331 IAudioClient_Release(ac);
332 CoTaskMemFree(pwfx);
333 return;
336 hr = IAudioClient_GetStreamLatency(ac, NULL);
337 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
339 hr = IAudioClient_GetStreamLatency(ac, &t2);
340 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
341 trace("Returned latency: %u.%04u ms\n",
342 (UINT)(t2/10000), (UINT)(t2 % 10000));
343 ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000),
344 "Latency < default period, delta %ldus\n", (long)((t2-t1)/10));
345 /* Native appears to add the engine period to the HW latency in shared mode */
347 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
348 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
350 hr = IAudioClient_SetEventHandle(ac, NULL);
351 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
353 hr = IAudioClient_SetEventHandle(ac, handle);
354 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
355 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
356 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
357 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
358 , "SetEventHandle returns %08x\n", hr);
360 hr = IAudioClient_Reset(ac);
361 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
363 hr = IAudioClient_Reset(ac);
364 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
366 hr = IAudioClient_Stop(ac);
367 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
369 hr = IAudioClient_Start(ac);
370 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
372 hr = IAudioClient_Start(ac);
373 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
375 IAudioClient_Release(ac);
377 CloseHandle(handle);
378 CoTaskMemFree(pwfx);
381 static void test_formats(AUDCLNT_SHAREMODE mode)
383 IAudioClient *ac;
384 HRESULT hr, hrs;
385 WAVEFORMATEX fmt, *pwfx, *pwfx2;
386 int i;
388 fmt.wFormatTag = WAVE_FORMAT_PCM;
389 fmt.cbSize = 0;
391 for(i = 0; i < NB_WIN_FORMATS; i++) {
392 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
393 NULL, (void**)&ac);
394 ok(hr == S_OK, "Activation failed with %08x\n", hr);
395 if(hr != S_OK)
396 continue;
398 hr = IAudioClient_GetMixFormat(ac, &pwfx);
399 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
401 fmt.nSamplesPerSec = win_formats[i][0];
402 fmt.wBitsPerSample = win_formats[i][1];
403 fmt.nChannels = win_formats[i][2];
404 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
405 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
407 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
408 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
409 hrs = hr;
410 /* Only shared mode suggests something ... GetMixFormat! */
411 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
412 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
413 /* 5:1 card exception when asked for 1 channel at mixer rate */
414 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
415 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
416 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
417 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
418 if (hr == S_OK)
419 trace("IsSupported(%s, %ux%2ux%u)\n",
420 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
421 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
423 /* Change GetMixFormat wBitsPerSample only => S_OK */
424 if (mode == AUDCLNT_SHAREMODE_SHARED
425 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
426 && fmt.nChannels == pwfx->nChannels)
427 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
429 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
430 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
431 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
432 if (pwfx2) {
433 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
434 pwfx2->nChannels == pwfx->nChannels &&
435 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
436 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
437 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
440 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
441 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
442 if ((hrs == S_OK) ^ (hr == S_OK))
443 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
444 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
445 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
446 if (mode == AUDCLNT_SHAREMODE_SHARED)
447 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
448 "Initialize(shared, %ux%2ux%u) returns %08x\n",
449 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
450 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
451 /* Unsupported format implies "create failed" and shadows "not allowed" */
452 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
453 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
454 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
455 else
456 /* On testbot 48000x16x1 claims support, but does not Initialize.
457 * Some cards Initialize 44100|48000x16x1 yet claim no support;
458 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
459 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
460 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == AUDCLNT_E_UNSUPPORTED_FORMAT ||
461 broken(hr == S_OK &&
462 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
463 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
464 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
465 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
467 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
468 * Release this ac and Activate a new one.
469 * A second call (with a known working format) would yield
470 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
471 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
472 * all subsequent calls until the audio engine service is restarted. */
474 CoTaskMemFree(pwfx2);
475 CoTaskMemFree(pwfx);
476 IAudioClient_Release(ac);
480 static void test_references(void)
482 IAudioClient *ac;
483 IAudioRenderClient *rc;
484 ISimpleAudioVolume *sav;
485 IAudioStreamVolume *asv;
486 IAudioClock *acl;
487 WAVEFORMATEX *pwfx;
488 HRESULT hr;
489 ULONG ref;
491 /* IAudioRenderClient */
492 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
493 NULL, (void**)&ac);
494 ok(hr == S_OK, "Activation failed with %08x\n", hr);
495 if(hr != S_OK)
496 return;
498 hr = IAudioClient_GetMixFormat(ac, &pwfx);
499 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
501 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
502 0, pwfx, NULL);
503 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
505 CoTaskMemFree(pwfx);
507 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
508 ok(hr == S_OK, "GetService failed: %08x\n", hr);
509 if(hr != S_OK) {
510 IAudioClient_Release(ac);
511 return;
514 IAudioRenderClient_AddRef(rc);
515 ref = IAudioRenderClient_Release(rc);
516 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
518 ref = IAudioClient_Release(ac);
519 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
521 ref = IAudioRenderClient_Release(rc);
522 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
524 /* ISimpleAudioVolume */
525 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
526 NULL, (void**)&ac);
527 ok(hr == S_OK, "Activation failed with %08x\n", hr);
528 if(hr != S_OK)
529 return;
531 hr = IAudioClient_GetMixFormat(ac, &pwfx);
532 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
534 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
535 0, pwfx, NULL);
536 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
538 CoTaskMemFree(pwfx);
540 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
541 ok(hr == S_OK, "GetService failed: %08x\n", hr);
543 ISimpleAudioVolume_AddRef(sav);
544 ref = ISimpleAudioVolume_Release(sav);
545 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
547 ref = IAudioClient_Release(ac);
548 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
550 ref = ISimpleAudioVolume_Release(sav);
551 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
553 /* IAudioClock */
554 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
555 NULL, (void**)&ac);
556 ok(hr == S_OK, "Activation failed with %08x\n", hr);
557 if(hr != S_OK)
558 return;
560 hr = IAudioClient_GetMixFormat(ac, &pwfx);
561 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
563 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
564 0, pwfx, NULL);
565 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
567 CoTaskMemFree(pwfx);
569 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
570 ok(hr == S_OK, "GetService failed: %08x\n", hr);
572 IAudioClock_AddRef(acl);
573 ref = IAudioClock_Release(acl);
574 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
576 ref = IAudioClient_Release(ac);
577 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
579 ref = IAudioClock_Release(acl);
580 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
582 /* IAudioStreamVolume */
583 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
584 NULL, (void**)&ac);
585 ok(hr == S_OK, "Activation failed with %08x\n", hr);
586 if(hr != S_OK)
587 return;
589 hr = IAudioClient_GetMixFormat(ac, &pwfx);
590 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
592 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
593 0, pwfx, NULL);
594 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
596 CoTaskMemFree(pwfx);
598 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
599 ok(hr == S_OK, "GetService failed: %08x\n", hr);
601 IAudioStreamVolume_AddRef(asv);
602 ref = IAudioStreamVolume_Release(asv);
603 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
605 ref = IAudioClient_Release(ac);
606 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
608 ref = IAudioStreamVolume_Release(asv);
609 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
612 static void test_event(void)
614 HANDLE event;
615 HRESULT hr;
616 DWORD r;
617 IAudioClient *ac;
618 WAVEFORMATEX *pwfx;
620 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
621 NULL, (void**)&ac);
622 ok(hr == S_OK, "Activation failed with %08x\n", hr);
623 if(hr != S_OK)
624 return;
626 hr = IAudioClient_GetMixFormat(ac, &pwfx);
627 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
629 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
630 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
631 0, pwfx, NULL);
632 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
634 CoTaskMemFree(pwfx);
636 event = CreateEventW(NULL, FALSE, FALSE, NULL);
637 ok(event != NULL, "CreateEvent failed\n");
639 hr = IAudioClient_Start(ac);
640 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "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), "SetEventHandle returns %08x\n", hr);
648 r = WaitForSingleObject(event, 40);
649 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
651 hr = IAudioClient_Start(ac);
652 ok(hr == S_OK, "Start failed: %08x\n", hr);
654 r = WaitForSingleObject(event, 20);
655 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
657 hr = IAudioClient_Stop(ac);
658 ok(hr == S_OK, "Stop failed: %08x\n", hr);
660 ok(ResetEvent(event), "ResetEvent\n");
662 /* Still receiving events! */
663 r = WaitForSingleObject(event, 20);
664 todo_wine ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
666 hr = IAudioClient_Reset(ac);
667 ok(hr == S_OK, "Reset failed: %08x\n", hr);
669 ok(ResetEvent(event), "ResetEvent\n");
671 r = WaitForSingleObject(event, 120);
672 todo_wine ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
674 hr = IAudioClient_SetEventHandle(ac, NULL);
675 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
677 r = WaitForSingleObject(event, 70);
678 todo_wine ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
680 /* test releasing a playing stream */
681 hr = IAudioClient_Start(ac);
682 ok(hr == S_OK, "Start failed: %08x\n", hr);
683 IAudioClient_Release(ac);
685 CloseHandle(event);
688 static void test_padding(void)
690 HRESULT hr;
691 IAudioClient *ac;
692 IAudioRenderClient *arc;
693 WAVEFORMATEX *pwfx;
694 REFERENCE_TIME minp, defp;
695 BYTE *buf, silence;
696 UINT32 psize, pad, written, i;
698 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
699 NULL, (void**)&ac);
700 ok(hr == S_OK, "Activation failed with %08x\n", hr);
701 if(hr != S_OK)
702 return;
704 hr = IAudioClient_GetMixFormat(ac, &pwfx);
705 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
706 if(hr != S_OK)
707 return;
709 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
710 0, 5000000, 0, pwfx, NULL);
711 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
712 if(hr != S_OK)
713 return;
715 if(pwfx->wBitsPerSample == 8)
716 silence = 128;
717 else
718 silence = 0;
720 /** GetDevicePeriod
721 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
722 * except when the HW/OS forces a particular alignment,
723 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
724 * 441 observed with Vista, 448 with w7 on the same HW! */
725 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
726 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
727 /* some wineXYZ.drv use 20ms, not seen on native */
728 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
729 "Expected 10ms default period: %u\n", (ULONG)defp);
730 ok(minp != 0, "Minimum period is 0\n");
731 ok(minp <= defp, "Mininum period is greater than default period\n");
733 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
734 ok(hr == S_OK, "GetService failed: %08x\n", hr);
736 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
738 written = 0;
739 hr = IAudioClient_GetCurrentPadding(ac, &pad);
740 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
741 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
743 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
744 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
745 ok(buf != NULL, "NULL buffer returned\n");
746 for(i = 0; i < psize * pwfx->nBlockAlign; ++i){
747 if(buf[i] != silence){
748 ok(0, "buffer has data in it already\n");
749 break;
753 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
754 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
755 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
756 /* MSDN instead documents buf remains untouched */
758 hr = IAudioClient_Reset(ac);
759 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
761 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
762 AUDCLNT_BUFFERFLAGS_SILENT);
763 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
764 if(hr == S_OK) written += psize;
766 hr = IAudioClient_GetCurrentPadding(ac, &pad);
767 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
768 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
770 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
772 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
773 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
774 ok(buf != NULL, "NULL buffer returned\n");
776 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
777 AUDCLNT_BUFFERFLAGS_SILENT);
778 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
779 written += psize;
781 hr = IAudioClient_GetCurrentPadding(ac, &pad);
782 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
783 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
785 /* overfull buffer. requested 1/2s buffer size, so try
786 * to get a 1/2s buffer, which should fail */
787 psize = pwfx->nSamplesPerSec / 2;
788 buf = (void*)0xDEADF00D;
789 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
790 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
791 ok(buf == NULL, "NULL expected %p\n", buf);
793 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
794 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
796 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
798 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
799 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
800 ok(buf != NULL, "NULL buffer returned\n");
802 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
803 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
805 buf = (void*)0xDEADF00D;
806 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
807 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
808 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
809 /* MSDN instead documents buf remains untouched */
811 buf = (void*)0xDEADF00D;
812 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
813 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
814 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
816 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
817 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
819 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
820 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
821 ok(buf != NULL, "NULL buffer returned\n");
823 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
824 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
826 hr = IAudioClient_GetCurrentPadding(ac, &pad);
827 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
828 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
830 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
831 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
832 ok(buf != NULL, "NULL buffer returned\n");
834 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
835 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
836 /* todo_wine means Wine may overwrite memory */
837 if(hr == S_OK) written += psize+1;
839 /* Buffer still hold */
840 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
841 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
842 if(hr == S_OK) written += psize/2;
844 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
845 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
847 hr = IAudioClient_GetCurrentPadding(ac, &pad);
848 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
849 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
851 CoTaskMemFree(pwfx);
853 IAudioRenderClient_Release(arc);
854 IAudioClient_Release(ac);
857 static void test_clock(int share)
859 HRESULT hr;
860 IAudioClient *ac;
861 IAudioClock *acl;
862 IAudioRenderClient *arc;
863 UINT64 freq, pos, pcpos0, pcpos, last;
864 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
865 BYTE *data;
866 WAVEFORMATEX *pwfx;
867 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
868 REFERENCE_TIME minp, defp, t1, t2;
869 REFERENCE_TIME duration = 5000000, period = 150000;
870 int i;
872 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
874 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
875 NULL, (void**)&ac);
876 ok(hr == S_OK, "Activation failed with %08x\n", hr);
877 if(hr != S_OK)
878 return;
880 hr = IAudioClient_GetMixFormat(ac, &pwfx);
881 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
882 if(hr != S_OK)
883 return;
885 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
886 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
887 ok(minp <= period, "desired period %u too small for %u\n", (ULONG)period, (ULONG)minp);
889 if (share) {
890 trace("Testing shared mode\n");
891 /* period is ignored */
892 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
893 0, duration, period, pwfx, NULL);
894 period = defp;
895 } else {
896 pwfx->wFormatTag = WAVE_FORMAT_PCM;
897 pwfx->nChannels = 2;
898 pwfx->cbSize = 0;
899 pwfx->wBitsPerSample = 16; /* no floating point */
900 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
901 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
902 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
904 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
905 0, duration, period, pwfx, NULL);
907 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
908 if (hr != S_OK) {
909 CoTaskMemFree(pwfx);
910 IAudioClient_Release(ac);
911 if(hr == AUDCLNT_E_DEVICE_IN_USE)
912 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
913 return;
916 /** GetStreamLatency
917 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
918 * Exclusive mode: testbot returns 2x period + a little, but
919 * some HDA drivers return 1x period, some + a little. */
920 hr = IAudioClient_GetStreamLatency(ac, &t2);
921 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
922 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
923 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000),
924 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
926 /** GetBufferSize
927 * BufferSize must be rounded up, maximum 2s says MSDN.
928 * Both is wrong. Rounding may lead to size a little smaller than duration;
929 * duration > 2s is accepted in shared mode.
930 * Shared mode: round solely w.r.t. mixer rate,
931 * duration is no multiple of period.
932 * Exclusive mode: size appears as a multiple of some fragment that
933 * is either the rounded period or a fixed constant like 1024,
934 * whatever the driver implements. */
935 hr = IAudioClient_GetBufferSize(ac, &gbsize);
936 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
938 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
939 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
940 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
941 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
942 /* fragment size (= period in frames) is rounded up.
943 * BufferSize must be rounded up, maximum 2s says MSDN
944 * but it is rounded down modulo fragment ! */
945 if (share)
946 ok(gbsize == bufsize,
947 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
948 else todo_wine
949 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
950 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
952 /* In shared mode, GetCurrentPadding decreases in multiples of
953 * fragment size (i.e. updated only at period ticks), whereas
954 * GetPosition appears to be reporting continuous positions.
955 * In exclusive mode, testbot behaves likewise, but native's Intel
956 * HDA driver shows no such deltas, GetCurrentPadding closely
957 * matches GetPosition, as in
958 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
960 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
961 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
963 hr = IAudioClock_GetFrequency(acl, &freq);
964 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
965 trace("Clock Frequency %u\n", (UINT)freq);
967 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
968 if (share) todo_wine
969 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
970 "Clock Frequency %u\n", (UINT)freq);
971 else
972 ok(freq == pwfx->nSamplesPerSec,
973 "Clock Frequency %u\n", (UINT)freq);
975 hr = IAudioClock_GetPosition(acl, NULL, NULL);
976 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
978 pcpos0 = 0;
979 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
980 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
981 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
982 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
984 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
985 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
987 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
988 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
990 avail = gbsize;
991 data = NULL;
992 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
993 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
994 trace("data at %p\n", data);
996 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
997 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
998 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
999 if(hr == S_OK) sum += avail;
1001 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1002 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1003 ok(pad == sum, "padding %u prior to start\n", pad);
1005 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1006 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1007 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
1009 hr = IAudioClient_Start(ac); /* #1 */
1010 ok(hr == S_OK, "Start failed: %08x\n", hr);
1012 Sleep(100);
1013 slept += 100;
1015 hr = IAudioClient_GetStreamLatency(ac, &t1);
1016 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1017 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1019 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1020 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1021 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1022 /* in rare cases is slept*1.1 not enough with dmix */
1023 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1024 last = pos;
1026 hr = IAudioClient_Stop(ac);
1027 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1029 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1030 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1031 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1032 last = pos;
1033 if(/*share &&*/ winetest_debug>1) todo_wine
1034 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1036 hr = IAudioClient_Start(ac); /* #2 */
1037 ok(hr == S_OK, "Start failed: %08x\n", hr);
1039 Sleep(100);
1040 slept += 100;
1042 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1043 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1044 trace("padding %u past sleep #2\n", pad);
1046 /** IAudioClient_Stop
1047 * Exclusive mode: the audio engine appears to drop frames,
1048 * bumping GetPosition to a higher value than time allows, even
1049 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1050 * Shared mode: no drop observed (or too small to be visible).
1051 * GetPosition = sum Released - GetCurrentPadding
1052 * Bugs: Some USB headset system drained the whole buffer, leaving
1053 * padding 0 and bumping pos to sum minus 17 frames! */
1055 hr = IAudioClient_Stop(ac);
1056 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1058 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1059 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1061 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1062 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1063 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1064 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1065 /* Prove that Stop must not drop frames (in shared mode). */
1066 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1067 if (share && pad > 0 && winetest_debug>1) todo_wine
1068 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1069 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1070 if(/*share &&*/ winetest_debug>1)
1071 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1072 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1073 last = pos;
1075 Sleep(100);
1076 slept += 100;
1078 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1079 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1080 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1082 /* Restart from 0 */
1083 hr = IAudioClient_Reset(ac);
1084 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1085 slept = sum = 0;
1087 hr = IAudioClient_Reset(ac);
1088 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1090 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1091 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1092 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1093 ok(pcpos > pcpos0, "pcpos should increase\n");
1095 avail = gbsize; /* implies GetCurrentPadding == 0 */
1096 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1097 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1098 trace("data at %p\n", data);
1100 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1101 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1102 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1103 if(hr == S_OK) sum += avail;
1105 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1106 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1107 ok(pad == sum, "padding %u prior to start\n", pad);
1109 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1110 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1111 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1112 last = pos;
1114 hr = IAudioClient_Start(ac); /* #3 */
1115 ok(hr == S_OK, "Start failed: %08x\n", hr);
1117 Sleep(100);
1118 slept += 100;
1120 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1121 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1122 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1123 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1124 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1125 if (winetest_debug>1)
1126 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1127 else
1128 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1129 last = pos;
1131 hr = IAudioClient_Reset(ac);
1132 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1134 hr = IAudioClient_Stop(ac);
1135 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1137 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1138 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1140 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1141 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1142 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1143 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1144 ok(pcpos > pcpos0, "pcpos should increase\n");
1145 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1146 if (pad > 0 && winetest_debug>1) todo_wine
1147 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1148 if(winetest_debug>1)
1149 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1150 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1151 last = pos;
1153 /* Begin the big loop */
1154 hr = IAudioClient_Reset(ac);
1155 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1156 slept = last = sum = 0;
1157 pcpos0 = pcpos;
1159 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1161 hr = IAudioClient_Reset(ac);
1162 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1164 hr = IAudioClient_Start(ac);
1165 ok(hr == S_OK, "Start failed: %08x\n", hr);
1167 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1168 data = NULL;
1169 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1170 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1171 trace("data at %p for prefill %u\n", data, avail);
1173 if (winetest_debug>2) {
1174 hr = IAudioClient_Stop(ac);
1175 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1177 Sleep(20);
1178 slept += 20;
1180 hr = IAudioClient_Reset(ac);
1181 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1183 hr = IAudioClient_Start(ac);
1184 ok(hr == S_OK, "Start failed: %08x\n", hr);
1187 /* Despite passed time, data must still point to valid memory... */
1188 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1189 wave_generate_tone(pwfx, data, avail));
1190 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1191 if(hr == S_OK) sum += avail;
1193 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1194 * mixer may still have a little data. We believe an underrun will occur
1195 * when the mixer finds GCP smaller than a period size at the *end* of a
1196 * period cycle, i.e. shortly before calling SetEvent to signal the app
1197 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1198 * with no data written for over a period causes an underrun. */
1200 Sleep(350);
1201 slept += 350;
1202 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1203 trace("hpctime %u after %ums\n",
1204 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1206 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1207 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1208 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1209 last = pos;
1211 for(i=0; i < 9; i++) {
1212 Sleep(100);
1213 slept += 100;
1215 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1216 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1218 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1219 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1221 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1222 trace("hpctime %u pcpos %u\n",
1223 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1224 (ULONG)((pcpos-pcpos0)/10000));
1226 /* Use sum-pad to see whether position is ahead padding or not. */
1227 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1228 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1229 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1230 if (winetest_debug>1) {
1231 /* Padding does not lag behind by much */
1232 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1233 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1234 if (pad) /* not in case of underrun */
1235 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1236 "Position delta %ld not regular\n", (long)(pos-last));
1238 last = pos;
1240 hr = IAudioClient_GetStreamLatency(ac, &t1);
1241 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1242 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1244 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1245 data = NULL;
1246 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1247 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1248 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1249 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1250 if(hr == S_OK && i) todo_wine ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1251 /* Only the first iteration should allow that large a buffer
1252 * as prefill was drained during the first 350+100ms sleep.
1253 * Afterwards, only 100ms of data should find room per iteration. */
1255 if(hr == S_OK) {
1256 trace("data at %p\n", data);
1257 } else {
1258 avail = gbsize - pad;
1259 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1260 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1261 trace("data at %p (small %u)\n", data, avail);
1263 ok(data != NULL, "NULL buffer returned\n");
1264 if(i % 3 && !winetest_interactive) {
1265 memset(data, 0, avail * pwfx->nBlockAlign);
1266 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1267 } else {
1268 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1269 wave_generate_tone(pwfx, data, avail));
1271 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1272 if(hr == S_OK) sum += avail;
1275 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1276 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1277 trace("position %u\n", (UINT)pos);
1279 Sleep(1000); /* 500ms buffer underrun past full buffer */
1281 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1282 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1284 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1285 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1286 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1288 if (share) {
1289 /* Following underrun, all samples were played */
1290 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1291 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1292 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1293 } else {
1294 /* Vista and w2k8 leave partial fragments behind */
1295 ok(pad == 0 /* w7, w2k8R2 */||
1296 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1297 /* expect at most 5 fragments (75ms) away */
1298 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1299 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1300 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1303 hr = IAudioClient_GetStreamLatency(ac, &t1);
1304 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1305 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1307 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1308 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1310 hr = IAudioClient_Stop(ac);
1311 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1313 CoTaskMemFree(pwfx);
1315 IAudioClock_Release(acl);
1316 IAudioRenderClient_Release(arc);
1317 IAudioClient_Release(ac);
1320 static void test_session(void)
1322 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1323 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1324 IMMDevice *cap_dev;
1325 GUID ses1_guid;
1326 AudioSessionState state;
1327 WAVEFORMATEX *pwfx;
1328 ULONG ref;
1329 HRESULT hr;
1331 hr = CoCreateGuid(&ses1_guid);
1332 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1334 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1335 NULL, (void**)&ses1_ac1);
1336 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1337 if (FAILED(hr)) return;
1339 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1340 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1342 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1343 0, 5000000, 0, pwfx, &ses1_guid);
1344 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1346 if(hr == S_OK){
1347 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1348 NULL, (void**)&ses1_ac2);
1349 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1351 if(hr != S_OK){
1352 skip("Unable to open the same device twice. Skipping session tests\n");
1354 ref = IAudioClient_Release(ses1_ac1);
1355 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1356 CoTaskMemFree(pwfx);
1357 return;
1360 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1361 0, 5000000, 0, pwfx, &ses1_guid);
1362 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1364 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1365 eMultimedia, &cap_dev);
1366 if(hr == S_OK){
1367 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1368 NULL, (void**)&cap_ac);
1369 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1370 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1371 IMMDevice_Release(cap_dev);
1373 if(hr == S_OK){
1374 WAVEFORMATEX *cap_pwfx;
1376 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1377 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1379 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1380 0, 5000000, 0, cap_pwfx, &ses1_guid);
1381 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1382 CoTaskMemFree(cap_pwfx);
1384 if(hr == S_OK){
1385 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1386 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1387 if(FAILED(hr))
1388 cap_ctl = NULL;
1389 }else
1390 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1392 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1393 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1395 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1396 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1398 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1399 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1400 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1401 ref = IAudioSessionControl2_Release(ses1_ctl2);
1402 ok(ref != 0, "AudioSessionControl was destroyed\n");
1404 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1405 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1407 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1408 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1410 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1411 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1412 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1414 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1415 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1416 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1418 if(cap_ctl){
1419 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1420 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1421 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1424 hr = IAudioClient_Start(ses1_ac1);
1425 ok(hr == S_OK, "Start failed: %08x\n", hr);
1427 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1428 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1429 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1431 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1432 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1433 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1435 if(cap_ctl){
1436 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1437 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1438 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1441 hr = IAudioClient_Stop(ses1_ac1);
1442 ok(hr == S_OK, "Start failed: %08x\n", hr);
1444 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1445 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1446 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1448 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1449 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1450 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1452 if(cap_ctl){
1453 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1454 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1455 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1457 hr = IAudioClient_Start(cap_ac);
1458 ok(hr == S_OK, "Start failed: %08x\n", hr);
1460 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1461 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1462 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1464 hr = IAudioSessionControl2_GetState(ses1_ctl2, &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(cap_ctl, &state);
1469 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1470 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1472 hr = IAudioClient_Stop(cap_ac);
1473 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1475 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1476 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1477 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1479 hr = IAudioSessionControl2_GetState(ses1_ctl2, &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(cap_ctl, &state);
1484 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1485 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1487 ref = IAudioSessionControl2_Release(cap_ctl);
1488 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1490 ref = IAudioClient_Release(cap_ac);
1491 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1494 ref = IAudioSessionControl2_Release(ses1_ctl);
1495 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1497 ref = IAudioClient_Release(ses1_ac1);
1498 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1500 ref = IAudioClient_Release(ses1_ac2);
1501 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1503 /* we've released all of our IAudioClient references, so check GetState */
1504 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1505 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1506 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1508 ref = IAudioSessionControl2_Release(ses1_ctl2);
1509 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1511 CoTaskMemFree(pwfx);
1514 static void test_streamvolume(void)
1516 IAudioClient *ac;
1517 IAudioStreamVolume *asv;
1518 WAVEFORMATEX *fmt;
1519 UINT32 chans, i;
1520 HRESULT hr;
1521 float vol, *vols;
1523 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1524 NULL, (void**)&ac);
1525 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1526 if(hr != S_OK)
1527 return;
1529 hr = IAudioClient_GetMixFormat(ac, &fmt);
1530 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1532 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1533 0, fmt, NULL);
1534 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1536 if(hr == S_OK){
1537 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1538 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1540 if(hr != S_OK){
1541 IAudioClient_Release(ac);
1542 CoTaskMemFree(fmt);
1543 return;
1546 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1547 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1549 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1550 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1551 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1553 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1554 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1556 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1557 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1559 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1560 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1562 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1563 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1564 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1566 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1567 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1569 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1570 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1572 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1573 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1575 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1576 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1578 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1579 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1580 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1582 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1583 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1585 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1586 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1588 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1589 ok(vols != NULL, "HeapAlloc failed\n");
1591 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1592 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1594 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1595 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1596 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1597 for(i = 1; i < fmt->nChannels; ++i)
1598 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1600 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1601 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1603 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1604 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1606 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1607 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1609 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1610 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1612 HeapFree(GetProcessHeap(), 0, vols);
1613 IAudioStreamVolume_Release(asv);
1614 IAudioClient_Release(ac);
1615 CoTaskMemFree(fmt);
1618 static void test_channelvolume(void)
1620 IAudioClient *ac;
1621 IChannelAudioVolume *acv;
1622 WAVEFORMATEX *fmt;
1623 UINT32 chans, i;
1624 HRESULT hr;
1625 float vol, *vols;
1627 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1628 NULL, (void**)&ac);
1629 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1630 if(hr != S_OK)
1631 return;
1633 hr = IAudioClient_GetMixFormat(ac, &fmt);
1634 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1636 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1637 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1638 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1640 if(hr == S_OK){
1641 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1642 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1644 if(hr != S_OK){
1645 IAudioClient_Release(ac);
1646 CoTaskMemFree(fmt);
1647 return;
1650 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1651 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1653 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1654 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1655 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1657 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1658 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1660 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1661 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1663 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1664 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1666 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1667 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1668 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1670 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1671 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1673 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1674 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1676 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1677 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1679 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1680 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1682 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1683 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1684 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1686 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1687 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1689 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1690 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1692 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1693 ok(vols != NULL, "HeapAlloc failed\n");
1695 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1696 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1698 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1699 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1700 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1701 for(i = 1; i < fmt->nChannels; ++i)
1702 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1704 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1705 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1707 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1708 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1710 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1711 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1713 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1714 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1716 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1717 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1719 HeapFree(GetProcessHeap(), 0, vols);
1720 IChannelAudioVolume_Release(acv);
1721 IAudioClient_Release(ac);
1722 CoTaskMemFree(fmt);
1725 static void test_simplevolume(void)
1727 IAudioClient *ac;
1728 ISimpleAudioVolume *sav;
1729 WAVEFORMATEX *fmt;
1730 HRESULT hr;
1731 float vol;
1732 BOOL mute;
1734 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1735 NULL, (void**)&ac);
1736 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1737 if(hr != S_OK)
1738 return;
1740 hr = IAudioClient_GetMixFormat(ac, &fmt);
1741 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1743 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1744 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1745 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1747 if(hr == S_OK){
1748 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1749 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1751 if(hr != S_OK){
1752 IAudioClient_Release(ac);
1753 CoTaskMemFree(fmt);
1754 return;
1757 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1758 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1760 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1761 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1762 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1764 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1765 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1767 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1768 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1770 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1771 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1773 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1774 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1775 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1777 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1778 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1780 mute = TRUE;
1781 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1782 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1783 ok(mute == FALSE, "Session is already muted\n");
1785 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1786 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1788 mute = FALSE;
1789 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1790 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1791 ok(mute == TRUE, "Session should have been muted\n");
1793 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1794 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1795 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1797 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1798 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1800 mute = FALSE;
1801 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1802 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1803 ok(mute == TRUE, "Session should have been muted\n");
1805 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1806 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1808 ISimpleAudioVolume_Release(sav);
1809 IAudioClient_Release(ac);
1810 CoTaskMemFree(fmt);
1813 static void test_volume_dependence(void)
1815 IAudioClient *ac, *ac2;
1816 ISimpleAudioVolume *sav;
1817 IChannelAudioVolume *cav;
1818 IAudioStreamVolume *asv;
1819 WAVEFORMATEX *fmt;
1820 HRESULT hr;
1821 float vol;
1822 GUID session;
1823 UINT32 nch;
1825 hr = CoCreateGuid(&session);
1826 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1828 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1829 NULL, (void**)&ac);
1830 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1831 if(hr != S_OK)
1832 return;
1834 hr = IAudioClient_GetMixFormat(ac, &fmt);
1835 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1837 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1838 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1839 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1841 if(hr == S_OK){
1842 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1843 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1845 if(hr != S_OK){
1846 IAudioClient_Release(ac);
1847 CoTaskMemFree(fmt);
1848 return;
1851 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1852 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
1854 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1855 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1857 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1858 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1860 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1861 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1863 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1864 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1866 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1867 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1868 ok(fabsf(vol - 0.2) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1870 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1871 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1872 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1874 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1875 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1876 ok(fabsf(vol - 0.6) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1878 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1879 NULL, (void**)&ac2);
1880 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1882 if(hr == S_OK){
1883 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1884 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1885 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1886 if(hr != S_OK)
1887 IAudioClient_Release(ac2);
1890 if(hr == S_OK){
1891 IChannelAudioVolume *cav2;
1892 IAudioStreamVolume *asv2;
1894 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1895 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1897 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1898 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1900 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1901 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1902 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1904 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1905 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1906 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1908 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1909 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1910 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1912 hr = IAudioStreamVolume_GetChannelCount(asv2, &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 IAudioStreamVolume_Release(asv2);
1917 IChannelAudioVolume_Release(cav2);
1918 IAudioClient_Release(ac2);
1919 }else
1920 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1922 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1923 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1925 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1926 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1928 CoTaskMemFree(fmt);
1929 ISimpleAudioVolume_Release(sav);
1930 IChannelAudioVolume_Release(cav);
1931 IAudioStreamVolume_Release(asv);
1932 IAudioClient_Release(ac);
1935 static void test_session_creation(void)
1937 IMMDevice *cap_dev;
1938 IAudioClient *ac;
1939 IAudioSessionManager *sesm;
1940 ISimpleAudioVolume *sav;
1941 GUID session_guid;
1942 float vol;
1943 HRESULT hr;
1944 WAVEFORMATEX *fmt;
1946 CoCreateGuid(&session_guid);
1948 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1949 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1950 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1951 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1953 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1954 FALSE, &sav);
1955 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1957 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1958 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1960 /* Release completely to show session persistence */
1961 ISimpleAudioVolume_Release(sav);
1962 IAudioSessionManager_Release(sesm);
1964 /* test if we can create a capture audioclient in the session we just
1965 * created from a SessionManager derived from a render device */
1966 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1967 eMultimedia, &cap_dev);
1968 if(hr == S_OK){
1969 WAVEFORMATEX *cap_pwfx;
1970 IAudioClient *cap_ac;
1971 ISimpleAudioVolume *cap_sav;
1972 IAudioSessionManager *cap_sesm;
1974 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1975 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1976 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1977 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1979 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1980 FALSE, &cap_sav);
1981 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1983 vol = 0.5f;
1984 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1985 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1986 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
1988 ISimpleAudioVolume_Release(cap_sav);
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);
2015 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
2017 ISimpleAudioVolume_Release(cap_sav);
2020 IAudioClient_Release(cap_ac);
2023 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2024 NULL, (void**)&ac);
2025 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2026 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2027 if(hr != S_OK)
2028 return;
2030 hr = IAudioClient_GetMixFormat(ac, &fmt);
2031 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2033 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2034 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2035 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2037 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2038 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2039 if(hr == S_OK){
2040 vol = 0.5f;
2041 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2042 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2043 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2045 ISimpleAudioVolume_Release(sav);
2048 CoTaskMemFree(fmt);
2049 IAudioClient_Release(ac);
2052 static void test_worst_case(void)
2054 HANDLE event;
2055 HRESULT hr;
2056 IAudioClient *ac;
2057 IAudioRenderClient *arc;
2058 IAudioClock *acl;
2059 WAVEFORMATEX *pwfx;
2060 REFERENCE_TIME defp;
2061 UINT64 freq, pos, pcpos0, pcpos;
2062 BYTE *data;
2063 DWORD r;
2064 UINT32 pad, fragment, sum;
2065 int i,j;
2067 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2068 NULL, (void**)&ac);
2069 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2070 if(hr != S_OK)
2071 return;
2073 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2074 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2076 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2077 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2078 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2079 if(hr != S_OK)
2080 return;
2082 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2083 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2085 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2087 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2088 ok(event != NULL, "CreateEvent failed\n");
2090 hr = IAudioClient_SetEventHandle(ac, event);
2091 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2093 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2094 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2096 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2097 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2099 hr = IAudioClock_GetFrequency(acl, &freq);
2100 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2102 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2103 sum = 0;
2104 trace("Should play %ums continuous tone with fragment size %u.\n",
2105 (ULONG)(defp/100), fragment);
2107 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2108 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2110 /* XAudio2 prefills one period, play without it */
2111 if(winetest_debug>2){
2112 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2113 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2115 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2116 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2117 if(hr == S_OK)
2118 sum += fragment;
2121 hr = IAudioClient_Start(ac);
2122 ok(hr == S_OK, "Start failed: %08x\n", hr);
2124 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2125 r = WaitForSingleObject(event, 60 + defp / 10000);
2126 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2128 /* the app has nearly one period time to feed data */
2129 Sleep((i % 10) * defp / 120000);
2131 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2132 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2134 /* XAudio2 writes only when there's little data left */
2135 if(pad <= fragment){
2136 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2137 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2139 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2140 wave_generate_tone(pwfx, data, fragment));
2141 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2142 if(hr == S_OK)
2143 sum += fragment;
2147 hr = IAudioClient_Stop(ac);
2148 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2150 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2151 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2153 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2154 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2156 Sleep(100);
2158 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2159 sum, sum/fragment, fragment, pad,
2160 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2161 (ULONG)((pcpos-pcpos0)/10000));
2163 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2164 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2166 hr = IAudioClient_Reset(ac);
2167 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2169 Sleep(250);
2172 CoTaskMemFree(pwfx);
2173 IAudioClient_Release(ac);
2174 IAudioClock_Release(acl);
2175 IAudioRenderClient_Release(arc);
2178 static void test_marshal(void)
2180 IStream *pStream;
2181 IAudioClient *ac, *acDest;
2182 IAudioRenderClient *rc, *rcDest;
2183 WAVEFORMATEX *pwfx;
2184 HRESULT hr;
2186 /* IAudioRenderClient */
2187 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2188 NULL, (void**)&ac);
2189 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2190 if(hr != S_OK)
2191 return;
2193 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2194 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2196 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2197 0, pwfx, NULL);
2198 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2200 CoTaskMemFree(pwfx);
2202 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2203 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2204 if(hr != S_OK) {
2205 IAudioClient_Release(ac);
2206 return;
2209 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2210 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2212 /* marshal IAudioClient */
2214 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2215 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2217 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2218 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2219 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2220 if (hr == S_OK)
2221 IAudioClient_Release(acDest);
2223 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2224 /* marshal IAudioRenderClient */
2226 hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2227 ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2229 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2230 hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2231 ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2232 if (hr == S_OK)
2233 IAudioRenderClient_Release(rcDest);
2236 IStream_Release(pStream);
2238 IAudioClient_Release(ac);
2239 IAudioRenderClient_Release(rc);
2243 START_TEST(render)
2245 HRESULT hr;
2247 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2248 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2249 if (FAILED(hr))
2251 skip("mmdevapi not available: 0x%08x\n", hr);
2252 goto cleanup;
2255 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2256 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2257 if (hr != S_OK || !dev)
2259 if (hr == E_NOTFOUND)
2260 skip("No sound card available\n");
2261 else
2262 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2263 goto cleanup;
2266 test_audioclient();
2267 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2268 test_formats(AUDCLNT_SHAREMODE_SHARED);
2269 test_references();
2270 test_marshal();
2271 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2272 trace("Please redirect output to a file.\n");
2273 test_event();
2274 test_padding();
2275 test_clock(1);
2276 test_clock(0);
2277 test_session();
2278 test_streamvolume();
2279 test_channelvolume();
2280 test_simplevolume();
2281 test_volume_dependence();
2282 test_session_creation();
2283 test_worst_case();
2285 IMMDevice_Release(dev);
2287 cleanup:
2288 if (mme)
2289 IMMDeviceEnumerator_Release(mme);
2290 CoUninitialize();