2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
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
22 * - IAudioClient with eRender and IAudioRenderClient
28 #include "wine/test.h"
38 #include "mmdeviceapi.h"
39 #include "audioclient.h"
40 #include "audiopolicy.h"
42 static const unsigned int win_formats[][4] = {
43 { 8000, 8, 1}, { 8000, 8, 2}, { 8000, 16, 1}, { 8000, 16, 2},
44 {11025, 8, 1}, {11025, 8, 2}, {11025, 16, 1}, {11025, 16, 2},
45 {12000, 8, 1}, {12000, 8, 2}, {12000, 16, 1}, {12000, 16, 2},
46 {16000, 8, 1}, {16000, 8, 2}, {16000, 16, 1}, {16000, 16, 2},
47 {22050, 8, 1}, {22050, 8, 2}, {22050, 16, 1}, {22050, 16, 2},
48 {44100, 8, 1}, {44100, 8, 2}, {44100, 16, 1}, {44100, 16, 2},
49 {48000, 8, 1}, {48000, 8, 2}, {48000, 16, 1}, {48000, 16, 2},
50 {96000, 8, 1}, {96000, 8, 2}, {96000, 16, 1}, {96000, 16, 2}
52 #define NB_WIN_FORMATS (sizeof(win_formats)/sizeof(*win_formats))
54 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
56 static IMMDeviceEnumerator *mme = NULL;
57 static IMMDevice *dev = NULL;
58 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
60 static inline const char *dbgstr_guid( const GUID *id )
63 sprintf(ret, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
64 id->Data1, id->Data2, id->Data3,
65 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
66 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
70 #define PI 3.14159265358979323846L
71 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
73 static double phase = 0.; /* normalized to unit, not 2*PI */
74 PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
78 if(!winetest_interactive)
79 return AUDCLNT_BUFFERFLAGS_SILENT;
80 if(wfxe->Format.wBitsPerSample != ((wfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
81 IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) ? 8 * sizeof(float) : 16))
82 return AUDCLNT_BUFFERFLAGS_SILENT;
84 for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
85 delta += .5/wfxe->Format.nChannels, cn++){
86 for(i = 0; i < frames; i++){
87 y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
88 /* assume alignment is granted */
89 if(wfxe->Format.wBitsPerSample == 16)
90 ((short*)data)[cn+i*wfxe->Format.nChannels] = y * 32767.9;
92 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
95 phase += 440.* frames / wfxe->Format.nSamplesPerSec;
96 phase -= floor(phase);
100 static void test_uninitialized(IAudioClient *ac)
106 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
109 hr = IAudioClient_GetBufferSize(ac, &num);
110 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
112 hr = IAudioClient_GetStreamLatency(ac, &t1);
113 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
115 hr = IAudioClient_GetCurrentPadding(ac, &num);
116 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
118 hr = IAudioClient_Start(ac);
119 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
121 hr = IAudioClient_Stop(ac);
122 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
124 hr = IAudioClient_Reset(ac);
125 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
127 hr = IAudioClient_SetEventHandle(ac, handle);
128 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
130 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
131 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
136 static void test_audioclient(void)
142 WAVEFORMATEX *pwfx, *pwfx2;
143 REFERENCE_TIME t1, t2;
146 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
148 ok(hr == S_OK, "Activation failed with %08x\n", hr);
152 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
154 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
155 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
157 unk = (void*)(LONG_PTR)0x12345678;
158 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
159 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
160 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
162 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
163 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
166 ref = IUnknown_Release(unk);
167 ok(ref == 1, "Released count is %u\n", ref);
170 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
171 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
174 ref = IUnknown_Release(unk);
175 ok(ref == 1, "Released count is %u\n", ref);
178 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
179 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
181 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
182 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
184 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
185 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
187 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
188 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
189 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
190 (UINT)(t1/10000), (UINT)(t1 % 10000),
191 (UINT)(t2/10000), (UINT)(t2 % 10000));
193 hr = IAudioClient_GetMixFormat(ac, NULL);
194 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
196 hr = IAudioClient_GetMixFormat(ac, &pwfx);
197 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
201 trace("pwfx: %p\n", pwfx);
202 trace("Tag: %04x\n", pwfx->wFormatTag);
203 trace("bits: %u\n", pwfx->wBitsPerSample);
204 trace("chan: %u\n", pwfx->nChannels);
205 trace("rate: %u\n", pwfx->nSamplesPerSec);
206 trace("align: %u\n", pwfx->nBlockAlign);
207 trace("extra: %u\n", pwfx->cbSize);
208 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
209 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
211 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
212 trace("Res: %u\n", pwfxe->Samples.wReserved);
213 trace("Mask: %x\n", pwfxe->dwChannelMask);
215 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
216 (IsEqualGUID(&pwfxe->SubFormat,
217 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
220 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
221 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
222 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
223 CoTaskMemFree(pwfx2);
225 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
226 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
228 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
229 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
231 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
232 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
233 "IsFormatSupported(Exclusive) call returns %08x\n", hr);
236 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
237 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
238 ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
239 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
241 if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
244 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
245 ok(hr == E_INVALIDARG/*w32*/ ||
246 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
247 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
250 test_uninitialized(ac);
252 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
253 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
255 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
256 ok(hr == E_INVALIDARG, "Initialize with invalid flags returns %08x\n", hr);
258 /* A period != 0 is ignored and the call succeeds.
259 * Since we can only initialize successfully once, skip those tests.
261 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
262 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
264 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
265 ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
269 hr = IAudioClient_GetBufferSize(ac, &num);
270 ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
272 trace("Initialize(duration=0) GetBufferSize is %u\n", num);
275 IAudioClient_Release(ac);
277 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
279 ok(hr == S_OK, "Activation failed with %08x\n", hr);
281 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
282 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
283 WAVEFORMATEX *fmt2 = NULL;
285 ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
287 fmtex->dwChannelMask = 0xffff;
289 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
290 ok(hr == S_OK, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
292 IAudioClient_Release(ac);
294 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
296 ok(hr == S_OK, "Activation failed with %08x\n", hr);
298 fmtex->dwChannelMask = 0;
300 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
301 ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
302 "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
303 ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
307 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
308 ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
310 IAudioClient_Release(ac);
312 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
314 ok(hr == S_OK, "Activation failed with %08x\n", hr);
318 hr = IAudioClient_GetMixFormat(ac, &pwfx);
319 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
321 skip("Skipping dwChannelMask tests\n");
323 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
324 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
327 IAudioClient_Release(ac);
332 hr = IAudioClient_GetStreamLatency(ac, NULL);
333 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
335 hr = IAudioClient_GetStreamLatency(ac, &t2);
336 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
337 trace("Returned latency: %u.%04u ms\n",
338 (UINT)(t2/10000), (UINT)(t2 % 10000));
339 ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000),
340 "Latency < default period, delta %ldus\n", (long)((t2-t1)/10));
341 /* Native appears to add the engine period to the HW latency in shared mode */
343 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
344 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
346 hr = IAudioClient_SetEventHandle(ac, NULL);
347 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
349 hr = IAudioClient_SetEventHandle(ac, handle);
350 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
351 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
352 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
353 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
354 , "SetEventHandle returns %08x\n", hr);
356 hr = IAudioClient_Reset(ac);
357 ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
359 hr = IAudioClient_Reset(ac);
360 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
362 hr = IAudioClient_Stop(ac);
363 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
365 hr = IAudioClient_Start(ac);
366 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
368 hr = IAudioClient_Start(ac);
369 ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
371 IAudioClient_Release(ac);
377 static void test_formats(AUDCLNT_SHAREMODE mode)
381 WAVEFORMATEX fmt, *pwfx, *pwfx2;
384 fmt.wFormatTag = WAVE_FORMAT_PCM;
387 for(i = 0; i < NB_WIN_FORMATS; i++) {
388 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
390 ok(hr == S_OK, "Activation failed with %08x\n", hr);
394 hr = IAudioClient_GetMixFormat(ac, &pwfx);
395 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
397 fmt.nSamplesPerSec = win_formats[i][0];
398 fmt.wBitsPerSample = win_formats[i][1];
399 fmt.nChannels = win_formats[i][2];
400 fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
401 fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
403 pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
404 hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
406 /* Only shared mode suggests something ... GetMixFormat! */
407 ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
408 ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
409 /* 5:1 card exception when asked for 1 channel at mixer rate */
410 pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
411 : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
412 "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
413 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
415 trace("IsSupported(%s, %ux%2ux%u)\n",
416 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
417 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
419 /* Change GetMixFormat wBitsPerSample only => S_OK */
420 if (mode == AUDCLNT_SHAREMODE_SHARED
421 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
422 && fmt.nChannels == pwfx->nChannels)
423 ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
425 ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
426 if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
427 pwfx2 = NULL; /* broken in Wine < 1.3.28 */
429 ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
430 pwfx2->nChannels == pwfx->nChannels &&
431 pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
432 "Suggestion %ux%2ux%u differs from GetMixFormat\n",
433 pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
436 /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
437 hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
438 if ((hrs == S_OK) ^ (hr == S_OK))
439 trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
440 mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
441 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
442 if (mode == AUDCLNT_SHAREMODE_SHARED)
443 ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
444 "Initialize(shared, %ux%2ux%u) returns %08x\n",
445 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
446 else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
447 /* Unsupported format implies "create failed" and shadows "not allowed" */
448 ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
449 "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
450 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
452 /* On testbot 48000x16x1 claims support, but does not Initialize.
453 * Some cards Initialize 44100|48000x16x1 yet claim no support;
454 * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
455 ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
456 : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || broken(hr == S_OK &&
457 ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
458 (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
459 "Initialize(exclus., %ux%2ux%u) returns %08x\n",
460 fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
462 /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
463 * Release this ac and Activate a new one.
464 * A second call (with a known working format) would yield
465 * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
466 * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
467 * all subsequent calls until the audio engine service is restarted. */
469 CoTaskMemFree(pwfx2);
471 IAudioClient_Release(ac);
475 static void test_references(void)
478 IAudioRenderClient *rc;
479 ISimpleAudioVolume *sav;
480 IAudioStreamVolume *asv;
486 /* IAudioRenderClient */
487 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
489 ok(hr == S_OK, "Activation failed with %08x\n", hr);
493 hr = IAudioClient_GetMixFormat(ac, &pwfx);
494 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
496 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
498 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
502 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
503 ok(hr == S_OK, "GetService failed: %08x\n", hr);
505 IAudioClient_Release(ac);
509 IAudioRenderClient_AddRef(rc);
510 ref = IAudioRenderClient_Release(rc);
511 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
513 ref = IAudioClient_Release(ac);
514 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
516 ref = IAudioRenderClient_Release(rc);
517 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
519 /* ISimpleAudioVolume */
520 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
522 ok(hr == S_OK, "Activation failed with %08x\n", hr);
526 hr = IAudioClient_GetMixFormat(ac, &pwfx);
527 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
529 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
531 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
535 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
536 ok(hr == S_OK, "GetService failed: %08x\n", hr);
538 ISimpleAudioVolume_AddRef(sav);
539 ref = ISimpleAudioVolume_Release(sav);
540 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
542 ref = IAudioClient_Release(ac);
543 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
545 ref = ISimpleAudioVolume_Release(sav);
546 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
549 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
551 ok(hr == S_OK, "Activation failed with %08x\n", hr);
555 hr = IAudioClient_GetMixFormat(ac, &pwfx);
556 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
558 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
560 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
564 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
565 ok(hr == S_OK, "GetService failed: %08x\n", hr);
567 IAudioClock_AddRef(acl);
568 ref = IAudioClock_Release(acl);
569 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
571 ref = IAudioClient_Release(ac);
572 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
574 ref = IAudioClock_Release(acl);
575 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
577 /* IAudioStreamVolume */
578 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
580 ok(hr == S_OK, "Activation failed with %08x\n", hr);
584 hr = IAudioClient_GetMixFormat(ac, &pwfx);
585 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
587 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
589 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
593 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
594 ok(hr == S_OK, "GetService failed: %08x\n", hr);
596 IAudioStreamVolume_AddRef(asv);
597 ref = IAudioStreamVolume_Release(asv);
598 ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
600 ref = IAudioClient_Release(ac);
601 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
603 ref = IAudioStreamVolume_Release(asv);
604 ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
607 static void test_event(void)
615 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
617 ok(hr == S_OK, "Activation failed with %08x\n", hr);
621 hr = IAudioClient_GetMixFormat(ac, &pwfx);
622 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
624 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
625 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
627 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
631 event = CreateEventW(NULL, FALSE, FALSE, NULL);
632 ok(event != NULL, "CreateEvent failed\n");
634 hr = IAudioClient_Start(ac);
635 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start failed: %08x\n", hr);
637 hr = IAudioClient_SetEventHandle(ac, event);
638 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
640 hr = IAudioClient_SetEventHandle(ac, event);
641 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), "SetEventHandle returns %08x\n", hr);
643 r = WaitForSingleObject(event, 40);
644 ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
646 hr = IAudioClient_Start(ac);
647 ok(hr == S_OK, "Start failed: %08x\n", hr);
649 r = WaitForSingleObject(event, 20);
650 ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
652 hr = IAudioClient_Stop(ac);
653 ok(hr == S_OK, "Stop failed: %08x\n", hr);
655 ok(ResetEvent(event), "ResetEvent\n");
657 /* Still receiving events! */
658 r = WaitForSingleObject(event, 20);
659 todo_wine ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
661 hr = IAudioClient_Reset(ac);
662 ok(hr == S_OK, "Reset failed: %08x\n", hr);
664 ok(ResetEvent(event), "ResetEvent\n");
666 r = WaitForSingleObject(event, 120);
667 todo_wine ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
669 hr = IAudioClient_SetEventHandle(ac, NULL);
670 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
672 r = WaitForSingleObject(event, 70);
673 todo_wine ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
675 /* test releasing a playing stream */
676 hr = IAudioClient_Start(ac);
677 ok(hr == S_OK, "Start failed: %08x\n", hr);
678 IAudioClient_Release(ac);
683 static void test_padding(void)
687 IAudioRenderClient *arc;
689 REFERENCE_TIME minp, defp;
691 UINT32 psize, pad, written;
693 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
695 ok(hr == S_OK, "Activation failed with %08x\n", hr);
699 hr = IAudioClient_GetMixFormat(ac, &pwfx);
700 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
704 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
705 0, 5000000, 0, pwfx, NULL);
706 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
711 * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
712 * except when the HW/OS forces a particular alignment,
713 * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
714 * 441 observed with Vista, 448 with w7 on the same HW! */
715 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
716 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
717 /* some wineXYZ.drv use 20ms, not seen on native */
718 ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
719 "Expected 10ms default period: %u\n", (ULONG)defp);
720 ok(minp != 0, "Minimum period is 0\n");
721 ok(minp <= defp, "Mininum period is greater than default period\n");
723 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
724 ok(hr == S_OK, "GetService failed: %08x\n", hr);
726 psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
729 hr = IAudioClient_GetCurrentPadding(ac, &pad);
730 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
731 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
733 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
734 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
735 ok(buf != NULL, "NULL buffer returned\n");
737 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
738 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
739 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
740 /* MSDN instead documents buf remains untouched */
742 hr = IAudioClient_Reset(ac);
743 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
745 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
746 AUDCLNT_BUFFERFLAGS_SILENT);
747 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
748 if(hr == S_OK) written += psize;
750 hr = IAudioClient_GetCurrentPadding(ac, &pad);
751 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
752 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
754 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
756 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
757 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
758 ok(buf != NULL, "NULL buffer returned\n");
760 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
761 AUDCLNT_BUFFERFLAGS_SILENT);
762 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
765 hr = IAudioClient_GetCurrentPadding(ac, &pad);
766 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
767 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
769 /* overfull buffer. requested 1/2s buffer size, so try
770 * to get a 1/2s buffer, which should fail */
771 psize = pwfx->nSamplesPerSec / 2;
772 buf = (void*)0xDEADF00D;
773 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
774 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
775 ok(buf == NULL, "NULL expected %p\n", buf);
777 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
778 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
780 psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
782 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
783 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
784 ok(buf != NULL, "NULL buffer returned\n");
786 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
787 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
789 buf = (void*)0xDEADF00D;
790 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
791 ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
792 ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
793 /* MSDN instead documents buf remains untouched */
795 buf = (void*)0xDEADF00D;
796 hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
797 ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
798 ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
800 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
801 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
803 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
804 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
805 ok(buf != NULL, "NULL buffer returned\n");
807 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
808 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
810 hr = IAudioClient_GetCurrentPadding(ac, &pad);
811 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
812 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
814 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
815 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
816 ok(buf != NULL, "NULL buffer returned\n");
818 hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
819 ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
820 /* todo_wine means Wine may overwrite memory */
821 if(hr == S_OK) written += psize+1;
823 /* Buffer still hold */
824 hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
825 ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
826 if(hr == S_OK) written += psize/2;
828 hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
829 ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
831 hr = IAudioClient_GetCurrentPadding(ac, &pad);
832 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
833 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
837 IAudioRenderClient_Release(arc);
838 IAudioClient_Release(ac);
841 static void test_clock(int share)
846 IAudioRenderClient *arc;
847 UINT64 freq, pos, pcpos0, pcpos, last;
848 UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
851 LARGE_INTEGER hpctime, hpctime0, hpcfreq;
852 REFERENCE_TIME minp, defp, t1, t2;
853 REFERENCE_TIME duration = 5000000, period = 150000;
856 ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
858 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
860 ok(hr == S_OK, "Activation failed with %08x\n", hr);
864 hr = IAudioClient_GetMixFormat(ac, &pwfx);
865 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
869 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
870 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
871 ok(minp <= period, "desired period %u to small for %u\n", (ULONG)period, (ULONG)minp);
874 trace("Testing shared mode\n");
875 /* period is ignored */
876 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
877 0, duration, period, pwfx, NULL);
880 pwfx->wFormatTag = WAVE_FORMAT_PCM;
883 pwfx->wBitsPerSample = 16; /* no floating point */
884 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
885 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
886 trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
888 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
889 0, duration, period, pwfx, NULL);
891 ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
894 IAudioClient_Release(ac);
895 if(hr == AUDCLNT_E_DEVICE_IN_USE)
896 skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
901 * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
902 * Exclusive mode: testbot returns 2x period + a little, but
903 * some HDA drivers return 1x period, some + a little. */
904 hr = IAudioClient_GetStreamLatency(ac, &t2);
905 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
906 trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
907 ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000),
908 "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
911 * BufferSize must be rounded up, maximum 2s says MSDN.
912 * Both is wrong. Rounding may lead to size a little smaller than duration;
913 * duration > 2s is accepted in shared mode.
914 * Shared mode: round solely w.r.t. mixer rate,
915 * duration is no multiple of period.
916 * Exclusive mode: size appears as a multiple of some fragment that
917 * is either the rounded period or a fixed constant like 1024,
918 * whatever the driver implements. */
919 hr = IAudioClient_GetBufferSize(ac, &gbsize);
920 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
922 bufsize = MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
923 fragment = MulDiv(period, pwfx->nSamplesPerSec, 10000000);
924 parts = MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
925 trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
926 /* fragment size (= period in frames) is rounded up.
927 * BufferSize must be rounded up, maximum 2s says MSDN
928 * but it is rounded down modulo fragment ! */
930 ok(gbsize == bufsize,
931 "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
933 ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
934 "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
936 /* In shared mode, GetCurrentPadding decreases in multiples of
937 * fragment size (i.e. updated only at period ticks), whereas
938 * GetPosition appears to be reporting continuous positions.
939 * In exclusive mode, testbot behaves likewise, but native's Intel
940 * HDA driver shows no such deltas, GetCurrentPadding closely
941 * matches GetPosition, as in
942 * GetCurrentPadding = GetPosition - frames held in mmdevapi */
944 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
945 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
947 hr = IAudioClock_GetFrequency(acl, &freq);
948 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
949 trace("Clock Frequency %u\n", (UINT)freq);
951 /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
953 ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
954 "Clock Frequency %u\n", (UINT)freq);
956 ok(freq == pwfx->nSamplesPerSec,
957 "Clock Frequency %u\n", (UINT)freq);
959 hr = IAudioClock_GetPosition(acl, NULL, NULL);
960 ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
963 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
964 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
965 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
966 ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
968 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
969 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
971 hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
972 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
976 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
977 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
978 trace("data at %p\n", data);
980 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
981 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
982 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
983 if(hr == S_OK) sum += avail;
985 hr = IAudioClient_GetCurrentPadding(ac, &pad);
986 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
987 ok(pad == sum, "padding %u prior to start\n", pad);
989 hr = IAudioClock_GetPosition(acl, &pos, NULL);
990 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
991 ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
993 hr = IAudioClient_Start(ac); /* #1 */
994 ok(hr == S_OK, "Start failed: %08x\n", hr);
999 hr = IAudioClient_GetStreamLatency(ac, &t1);
1000 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1001 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1003 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1004 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1005 ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1006 /* in rare cases is slept*1.1 not enough with dmix */
1007 ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1010 hr = IAudioClient_Stop(ac);
1011 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1013 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1014 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1015 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1017 if(/*share &&*/ winetest_debug>1) todo_wine
1018 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1020 hr = IAudioClient_Start(ac); /* #2 */
1021 ok(hr == S_OK, "Start failed: %08x\n", hr);
1026 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1027 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1028 trace("padding %u past sleep #2\n", pad);
1030 /** IAudioClient_Stop
1031 * Exclusive mode: the audio engine appears to drop frames,
1032 * bumping GetPosition to a higher value than time allows, even
1033 * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1034 * Shared mode: no drop observed (or too small to be visible).
1035 * GetPosition = sum Released - GetCurrentPadding
1036 * Bugs: Some USB headset system drained the whole buffer, leaving
1037 * padding 0 and bumping pos to sum minus 17 frames! */
1039 hr = IAudioClient_Stop(ac);
1040 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1042 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1043 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1045 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1046 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1047 trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1048 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1049 /* Prove that Stop must not drop frames (in shared mode). */
1050 ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1051 if (share && pad > 0 && winetest_debug>1) todo_wine
1052 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1053 /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1054 if(/*share &&*/ winetest_debug>1)
1055 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1056 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1062 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1063 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1064 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1066 /* Restart from 0 */
1067 hr = IAudioClient_Reset(ac);
1068 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1071 hr = IAudioClient_Reset(ac);
1072 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1074 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1075 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1076 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1077 ok(pcpos > pcpos0, "pcpos should increase\n");
1079 avail = gbsize; /* implies GetCurrentPadding == 0 */
1080 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1081 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1082 trace("data at %p\n", data);
1084 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1085 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1086 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1087 if(hr == S_OK) sum += avail;
1089 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1090 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1091 ok(pad == sum, "padding %u prior to start\n", pad);
1093 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1094 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1095 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1098 hr = IAudioClient_Start(ac); /* #3 */
1099 ok(hr == S_OK, "Start failed: %08x\n", hr);
1104 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1105 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1106 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1107 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1108 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1109 if (winetest_debug>1)
1110 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1112 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1115 hr = IAudioClient_Reset(ac);
1116 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1118 hr = IAudioClient_Stop(ac);
1119 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1121 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1122 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1124 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1125 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1126 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1127 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1128 ok(pcpos > pcpos0, "pcpos should increase\n");
1129 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1130 if (pad > 0 && winetest_debug>1) todo_wine
1131 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1132 if(winetest_debug>1)
1133 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1134 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1137 /* Begin the big loop */
1138 hr = IAudioClient_Reset(ac);
1139 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1140 slept = last = sum = 0;
1143 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1145 hr = IAudioClient_Reset(ac);
1146 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1148 hr = IAudioClient_Start(ac);
1149 ok(hr == S_OK, "Start failed: %08x\n", hr);
1151 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1153 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1154 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1155 trace("data at %p for prefill %u\n", data, avail);
1157 if (winetest_debug>2) {
1158 hr = IAudioClient_Stop(ac);
1159 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1164 hr = IAudioClient_Reset(ac);
1165 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1167 hr = IAudioClient_Start(ac);
1168 ok(hr == S_OK, "Start failed: %08x\n", hr);
1171 /* Despite passed time, data must still point to valid memory... */
1172 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1173 wave_generate_tone(pwfx, data, avail));
1174 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1175 if(hr == S_OK) sum += avail;
1177 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1178 * mixer may still have a little data. We believe an underrun will occur
1179 * when the mixer finds GCP smaller than a period size at the *end* of a
1180 * period cycle, i.e. shortly before calling SetEvent to signal the app
1181 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1182 * with no data written for over a period causes an underrun. */
1186 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1187 trace("hpctime %u after %ums\n",
1188 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1190 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1191 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1192 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1195 for(i=0; i < 9; i++) {
1199 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1200 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1202 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1203 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1205 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1206 trace("hpctime %u pcpos %u\n",
1207 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1208 (ULONG)((pcpos-pcpos0)/10000));
1210 /* Use sum-pad to see whether position is ahead padding or not. */
1211 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1212 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1213 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1214 if (winetest_debug>1) {
1215 /* Padding does not lag behind by much */
1216 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1217 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1218 if (pad) /* not in case of underrun */
1219 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1220 "Position delta %ld not regular\n", (long)(pos-last));
1224 hr = IAudioClient_GetStreamLatency(ac, &t1);
1225 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1226 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1228 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1230 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1231 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1232 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1233 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1234 if(hr == S_OK && i) todo_wine ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1235 /* Only the first iteration should allow that large a buffer
1236 * as prefill was drained during the first 350+100ms sleep.
1237 * Afterwards, only 100ms of data should find room per iteration. */
1240 trace("data at %p\n", data);
1242 avail = gbsize - pad;
1243 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1244 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1245 trace("data at %p (small %u)\n", data, avail);
1247 ok(data != NULL, "NULL buffer returned\n");
1248 if(i % 3 && !winetest_interactive) {
1249 memset(data, 0, avail * pwfx->nBlockAlign);
1250 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1252 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1253 wave_generate_tone(pwfx, data, avail));
1255 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1256 if(hr == S_OK) sum += avail;
1259 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1260 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1261 trace("position %u\n", (UINT)pos);
1263 Sleep(1000); /* 500ms buffer underrun past full buffer */
1265 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1266 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1268 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1269 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1270 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1273 /* Following underrun, all samples were played */
1274 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1275 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1276 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1278 /* Vista and w2k8 leave partial fragments behind */
1279 ok(pad == 0 /* w7, w2k8R2 */||
1280 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1281 /* expect at most 5 fragments (75ms) away */
1282 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1283 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1284 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1287 hr = IAudioClient_GetStreamLatency(ac, &t1);
1288 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1289 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1291 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1292 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1294 hr = IAudioClient_Stop(ac);
1295 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1297 CoTaskMemFree(pwfx);
1299 IAudioClock_Release(acl);
1300 IAudioRenderClient_Release(arc);
1301 IAudioClient_Release(ac);
1304 static void test_session(void)
1306 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1307 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1310 AudioSessionState state;
1315 hr = CoCreateGuid(&ses1_guid);
1316 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1318 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1319 NULL, (void**)&ses1_ac1);
1320 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1321 if (FAILED(hr)) return;
1323 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1324 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1326 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1327 0, 5000000, 0, pwfx, &ses1_guid);
1328 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1331 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1332 NULL, (void**)&ses1_ac2);
1333 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1336 skip("Unable to open the same device twice. Skipping session tests\n");
1338 ref = IAudioClient_Release(ses1_ac1);
1339 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1340 CoTaskMemFree(pwfx);
1344 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1345 0, 5000000, 0, pwfx, &ses1_guid);
1346 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1348 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1349 eMultimedia, &cap_dev);
1351 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1352 NULL, (void**)&cap_ac);
1353 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1354 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1355 IMMDevice_Release(cap_dev);
1358 WAVEFORMATEX *cap_pwfx;
1360 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1361 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1363 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1364 0, 5000000, 0, cap_pwfx, &ses1_guid);
1365 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1366 CoTaskMemFree(cap_pwfx);
1369 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1370 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1374 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1376 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1377 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1379 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1380 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1382 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1383 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1384 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1385 ref = IAudioSessionControl2_Release(ses1_ctl2);
1386 ok(ref != 0, "AudioSessionControl was destroyed\n");
1388 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1389 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1391 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1392 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1394 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1395 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1396 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1398 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1399 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1400 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1403 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1404 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1405 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1408 hr = IAudioClient_Start(ses1_ac1);
1409 ok(hr == S_OK, "Start failed: %08x\n", hr);
1411 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1412 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1413 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1415 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1416 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1417 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1420 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1421 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1422 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1425 hr = IAudioClient_Stop(ses1_ac1);
1426 ok(hr == S_OK, "Start failed: %08x\n", hr);
1428 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1429 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1430 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1432 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1433 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1434 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1437 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1438 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1439 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1441 hr = IAudioClient_Start(cap_ac);
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 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1453 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1454 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1456 hr = IAudioClient_Stop(cap_ac);
1457 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1459 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1460 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1461 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1463 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1464 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1465 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1467 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1468 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1469 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1471 ref = IAudioSessionControl2_Release(cap_ctl);
1472 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1474 ref = IAudioClient_Release(cap_ac);
1475 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1478 ref = IAudioSessionControl2_Release(ses1_ctl);
1479 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1481 ref = IAudioClient_Release(ses1_ac1);
1482 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1484 ref = IAudioClient_Release(ses1_ac2);
1485 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1487 /* we've released all of our IAudioClient references, so check GetState */
1488 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1489 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1490 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1492 ref = IAudioSessionControl2_Release(ses1_ctl2);
1493 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1495 CoTaskMemFree(pwfx);
1498 static void test_streamvolume(void)
1501 IAudioStreamVolume *asv;
1507 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1509 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1513 hr = IAudioClient_GetMixFormat(ac, &fmt);
1514 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1516 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1518 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1521 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1522 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1525 IAudioClient_Release(ac);
1530 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1531 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1533 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1534 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1535 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1537 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1538 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1540 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1541 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1543 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1544 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1546 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1547 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1548 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1550 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1551 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1553 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1554 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1556 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1557 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1559 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1560 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1562 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1563 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1564 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1566 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1567 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1569 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1570 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1572 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1573 ok(vols != NULL, "HeapAlloc failed\n");
1575 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1576 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1578 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1579 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1580 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1581 for(i = 1; i < fmt->nChannels; ++i)
1582 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1584 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1585 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1587 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1588 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1590 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1591 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1593 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1594 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1596 HeapFree(GetProcessHeap(), 0, vols);
1597 IAudioStreamVolume_Release(asv);
1598 IAudioClient_Release(ac);
1602 static void test_channelvolume(void)
1605 IChannelAudioVolume *acv;
1611 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1613 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1617 hr = IAudioClient_GetMixFormat(ac, &fmt);
1618 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1620 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1621 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1622 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1625 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1626 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1629 IAudioClient_Release(ac);
1634 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1635 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1637 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1638 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1639 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1641 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1642 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1644 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1645 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1647 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1648 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1650 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1651 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1652 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1654 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1655 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1657 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1658 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1660 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1661 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1663 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1664 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1666 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1667 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1668 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1670 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1671 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1673 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1674 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1676 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1677 ok(vols != NULL, "HeapAlloc failed\n");
1679 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1680 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1682 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1683 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1684 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1685 for(i = 1; i < fmt->nChannels; ++i)
1686 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1688 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1689 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1691 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1692 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1694 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1695 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1697 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1698 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1700 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1701 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1703 HeapFree(GetProcessHeap(), 0, vols);
1704 IChannelAudioVolume_Release(acv);
1705 IAudioClient_Release(ac);
1709 static void test_simplevolume(void)
1712 ISimpleAudioVolume *sav;
1718 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1720 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1724 hr = IAudioClient_GetMixFormat(ac, &fmt);
1725 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1727 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1728 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1729 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1732 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1733 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1736 IAudioClient_Release(ac);
1741 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1742 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1744 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1745 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1746 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1748 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1749 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1751 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1752 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1754 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1755 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1757 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1758 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1759 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1761 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1762 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1765 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1766 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1767 ok(mute == FALSE, "Session is already muted\n");
1769 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1770 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1773 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1774 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1775 ok(mute == TRUE, "Session should have been muted\n");
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_SetMasterVolume(sav, 1.f, NULL);
1782 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1785 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1786 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1787 ok(mute == TRUE, "Session should have been muted\n");
1789 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1790 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1792 ISimpleAudioVolume_Release(sav);
1793 IAudioClient_Release(ac);
1797 static void test_volume_dependence(void)
1799 IAudioClient *ac, *ac2;
1800 ISimpleAudioVolume *sav;
1801 IChannelAudioVolume *cav;
1802 IAudioStreamVolume *asv;
1809 hr = CoCreateGuid(&session);
1810 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1812 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1814 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1818 hr = IAudioClient_GetMixFormat(ac, &fmt);
1819 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1821 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1822 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1823 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1826 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1827 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1830 IAudioClient_Release(ac);
1835 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1836 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
1838 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1839 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1841 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1842 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1844 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1845 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1847 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1848 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1850 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1851 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1852 ok(fabsf(vol - 0.2) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1854 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1855 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1856 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1858 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1859 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1860 ok(fabsf(vol - 0.6) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1862 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1863 NULL, (void**)&ac2);
1864 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1867 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1868 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1869 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1871 IAudioClient_Release(ac2);
1875 IChannelAudioVolume *cav2;
1876 IAudioStreamVolume *asv2;
1878 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1879 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1881 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1882 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1884 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1885 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1886 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1888 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1889 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1890 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1892 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1893 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1894 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1896 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1897 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1898 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1900 IAudioStreamVolume_Release(asv2);
1901 IChannelAudioVolume_Release(cav2);
1902 IAudioClient_Release(ac2);
1904 skip("Unable to open the same device twice. Skipping session volume control tests\n");
1906 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1907 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1909 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1910 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1913 ISimpleAudioVolume_Release(sav);
1914 IChannelAudioVolume_Release(cav);
1915 IAudioStreamVolume_Release(asv);
1916 IAudioClient_Release(ac);
1919 static void test_session_creation(void)
1923 IAudioSessionManager *sesm;
1924 ISimpleAudioVolume *sav;
1930 CoCreateGuid(&session_guid);
1932 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1933 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1934 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1935 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1937 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1939 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1941 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1942 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1944 /* Release completely to show session persistence */
1945 ISimpleAudioVolume_Release(sav);
1946 IAudioSessionManager_Release(sesm);
1948 /* test if we can create a capture audioclient in the session we just
1949 * created from a SessionManager derived from a render device */
1950 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1951 eMultimedia, &cap_dev);
1953 WAVEFORMATEX *cap_pwfx;
1954 IAudioClient *cap_ac;
1955 ISimpleAudioVolume *cap_sav;
1956 IAudioSessionManager *cap_sesm;
1958 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1959 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1960 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1961 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1963 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1965 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1968 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1969 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1970 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
1972 ISimpleAudioVolume_Release(cap_sav);
1973 IAudioSessionManager_Release(cap_sesm);
1975 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1976 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1977 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1979 IMMDevice_Release(cap_dev);
1981 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1982 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1984 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1985 0, 5000000, 0, cap_pwfx, &session_guid);
1986 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1988 CoTaskMemFree(cap_pwfx);
1991 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
1993 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1997 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1998 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1999 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
2001 ISimpleAudioVolume_Release(cap_sav);
2004 IAudioClient_Release(cap_ac);
2007 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2009 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2010 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2014 hr = IAudioClient_GetMixFormat(ac, &fmt);
2015 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2017 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2018 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2019 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2021 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2022 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2025 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2026 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2027 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2029 ISimpleAudioVolume_Release(sav);
2033 IAudioClient_Release(ac);
2036 static void test_worst_case(void)
2041 IAudioRenderClient *arc;
2044 REFERENCE_TIME defp;
2045 UINT64 freq, pos, pcpos0, pcpos;
2048 UINT32 pad, fragment, sum;
2051 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2053 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2057 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2058 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2060 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2061 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2062 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2066 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2067 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2069 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2071 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2072 ok(event != NULL, "CreateEvent failed\n");
2074 hr = IAudioClient_SetEventHandle(ac, event);
2075 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2077 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2078 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2080 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2081 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2083 hr = IAudioClock_GetFrequency(acl, &freq);
2084 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2086 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2088 trace("Should play %ums continuous tone with fragment size %u.\n",
2089 (ULONG)(defp/100), fragment);
2091 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2092 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2094 /* XAudio2 prefills one period, play without it */
2095 if(winetest_debug>2){
2096 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2097 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2099 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2100 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2105 hr = IAudioClient_Start(ac);
2106 ok(hr == S_OK, "Start failed: %08x\n", hr);
2108 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2109 r = WaitForSingleObject(event, 60 + defp / 10000);
2110 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2112 /* the app has nearly one period time to feed data */
2113 Sleep((i % 10) * defp / 120000);
2115 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2116 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2118 /* XAudio2 writes only when there's little data left */
2119 if(pad <= fragment){
2120 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2121 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2123 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2124 wave_generate_tone(pwfx, data, fragment));
2125 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2131 hr = IAudioClient_Stop(ac);
2132 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2134 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2135 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2137 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2138 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2142 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2143 sum, sum/fragment, fragment, pad,
2144 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2145 (ULONG)((pcpos-pcpos0)/10000));
2147 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2148 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2150 hr = IAudioClient_Reset(ac);
2151 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2156 CoTaskMemFree(pwfx);
2157 IAudioClient_Release(ac);
2158 IAudioClock_Release(acl);
2159 IAudioRenderClient_Release(arc);
2166 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2167 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2170 skip("mmdevapi not available: 0x%08x\n", hr);
2174 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2175 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2176 if (hr != S_OK || !dev)
2178 if (hr == E_NOTFOUND)
2179 skip("No sound card available\n");
2181 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2186 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2187 test_formats(AUDCLNT_SHAREMODE_SHARED);
2189 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2190 trace("Please redirect output to a file.\n");
2196 test_streamvolume();
2197 test_channelvolume();
2198 test_simplevolume();
2199 test_volume_dependence();
2200 test_session_creation();
2203 IMMDevice_Release(dev);
2207 IMMDeviceEnumerator_Release(mme);