1 /*
2  * Copyright 2010 Maarten Lankhorst for CodeWeavers
3  *      2011-2012 Jörg Höhle
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 /* This test is for audio playback specific mechanisms
21  * Tests:
22  * - IAudioClient with eRender and IAudioRenderClient
23  */
24 
25 #include <math.h>
26 #include <stdio.h>
27 
28 #include "wine/test.h"
29 
30 #define COBJMACROS
31 
32 #ifdef STANDALONE
33 #include "initguid.h"
34 #endif
35 
36 #include "unknwn.h"
37 #include "uuids.h"
38 #include "mmdeviceapi.h"
39 #include "mmsystem.h"
40 #include "audioclient.h"
41 #include "audiopolicy.h"
42 #ifdef __REACTOS__
43 #include <initguid.h>
44 #endif
45 #include "endpointvolume.h"
46 
47 static const unsigned int win_formats[][4] = {
48     { 8000,  8, 1},   { 8000,  8, 2},   { 8000, 16, 1},   { 8000, 16, 2},
49     {11025,  8, 1},   {11025,  8, 2},   {11025, 16, 1},   {11025, 16, 2},
50     {12000,  8, 1},   {12000,  8, 2},   {12000, 16, 1},   {12000, 16, 2},
51     {16000,  8, 1},   {16000,  8, 2},   {16000, 16, 1},   {16000, 16, 2},
52     {22050,  8, 1},   {22050,  8, 2},   {22050, 16, 1},   {22050, 16, 2},
53     {44100,  8, 1},   {44100,  8, 2},   {44100, 16, 1},   {44100, 16, 2},
54     {48000,  8, 1},   {48000,  8, 2},   {48000, 16, 1},   {48000, 16, 2},
55     {96000,  8, 1},   {96000,  8, 2},   {96000, 16, 1},   {96000, 16, 2}
56 };
57 #define NB_WIN_FORMATS (sizeof(win_formats)/sizeof(*win_formats))
58 
59 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
60 
61 /* undocumented error code */
62 #define D3D11_ERROR_4E MAKE_HRESULT(SEVERITY_ERROR, FACILITY_DIRECT3D11, 0x4e)
63 
64 static IMMDeviceEnumerator *mme = NULL;
65 static IMMDevice *dev = NULL;
66 static HRESULT hexcl = S_OK; /* or AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED */
67 static BOOL win10 = FALSE;
68 
69 static const LARGE_INTEGER ullZero;
70 
71 #define PI 3.14159265358979323846L
72 static DWORD wave_generate_tone(PWAVEFORMATEX pwfx, BYTE* data, UINT32 frames)
73 {
74     static double phase = 0.; /* normalized to unit, not 2*PI */
75     PWAVEFORMATEXTENSIBLE wfxe = (PWAVEFORMATEXTENSIBLE)pwfx;
76     DWORD cn, i;
77     double delta, y;
78 
79     if(!winetest_interactive)
80         return AUDCLNT_BUFFERFLAGS_SILENT;
81     if(wfxe->Format.wBitsPerSample != ((wfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
82        IsEqualGUID(&wfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) ? 8 * sizeof(float) : 16))
83         return AUDCLNT_BUFFERFLAGS_SILENT;
84 
85     for(delta = phase, cn = 0; cn < wfxe->Format.nChannels;
86         delta += .5/wfxe->Format.nChannels, cn++){
87         for(i = 0; i < frames; i++){
88             y = sin(2*PI*(440.* i / wfxe->Format.nSamplesPerSec + delta));
89             /* assume alignment is granted */
90             if(wfxe->Format.wBitsPerSample == 16)
91                 ((short*)data)[cn+i*wfxe->Format.nChannels] = y * 32767.9;
92             else
93                 ((float*)data)[cn+i*wfxe->Format.nChannels] = y;
94         }
95     }
96     phase += 440.* frames / wfxe->Format.nSamplesPerSec;
97     phase -= floor(phase);
98     return 0;
99 }
100 
101 static void test_uninitialized(IAudioClient *ac)
102 {
103     HRESULT hr;
104     UINT32 num;
105     REFERENCE_TIME t1;
106 
107     HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
108     IUnknown *unk;
109 
110     hr = IAudioClient_GetBufferSize(ac, &num);
111     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
112 
113     hr = IAudioClient_GetStreamLatency(ac, &t1);
114     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
115 
116     hr = IAudioClient_GetCurrentPadding(ac, &num);
117     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
118 
119     hr = IAudioClient_Start(ac);
120     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
121 
122     hr = IAudioClient_Stop(ac);
123     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
124 
125     hr = IAudioClient_Reset(ac);
126     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
127 
128     hr = IAudioClient_SetEventHandle(ac, handle);
129     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
130 
131     hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
132     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
133 
134     CloseHandle(handle);
135 }
136 
137 static void test_audioclient(void)
138 {
139     IAudioClient *ac;
140     IUnknown *unk;
141     HRESULT hr;
142     ULONG ref;
143     WAVEFORMATEX *pwfx, *pwfx2;
144     REFERENCE_TIME t1, t2;
145     HANDLE handle;
146 
147     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
148             NULL, (void**)&ac);
149     ok(hr == S_OK, "Activation failed with %08x\n", hr);
150     if(hr != S_OK)
151         return;
152 
153     handle = CreateEventW(NULL, FALSE, FALSE, NULL);
154 
155     hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
156     ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
157 
158     unk = (void*)(LONG_PTR)0x12345678;
159     hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
160     ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
161     ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
162 
163     hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
164     ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
165     if (unk)
166     {
167         ref = IUnknown_Release(unk);
168         ok(ref == 1, "Released count is %u\n", ref);
169     }
170 
171     hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
172     ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
173     if (unk)
174     {
175         ref = IUnknown_Release(unk);
176         ok(ref == 1, "Released count is %u\n", ref);
177     }
178 
179     hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
180     ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
181 
182     hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
183     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
184 
185     hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
186     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
187 
188     hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
189     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
190     trace("Returned periods: %u.%04u ms %u.%04u ms\n",
191           (UINT)(t1/10000), (UINT)(t1 % 10000),
192           (UINT)(t2/10000), (UINT)(t2 % 10000));
193 
194     hr = IAudioClient_GetMixFormat(ac, NULL);
195     ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
196 
197     hr = IAudioClient_GetMixFormat(ac, &pwfx);
198     ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
199 
200     if (hr == S_OK)
201     {
202         trace("pwfx: %p\n", pwfx);
203         trace("Tag: %04x\n", pwfx->wFormatTag);
204         trace("bits: %u\n", pwfx->wBitsPerSample);
205         trace("chan: %u\n", pwfx->nChannels);
206         trace("rate: %u\n", pwfx->nSamplesPerSec);
207         trace("align: %u\n", pwfx->nBlockAlign);
208         trace("extra: %u\n", pwfx->cbSize);
209         ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
210         if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
211         {
212             WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
213             trace("Res: %u\n", pwfxe->Samples.wReserved);
214             trace("Mask: %x\n", pwfxe->dwChannelMask);
215             trace("Alg: %s\n",
216                   IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
217                   (IsEqualGUID(&pwfxe->SubFormat,
218                                &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
219         }
220 
221         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
222         ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
223         ok(pwfx2 == NULL, "pwfx2 is non-null\n");
224         CoTaskMemFree(pwfx2);
225 
226         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
227         ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
228 
229         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
230         ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
231 
232         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
233         ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED,
234            "IsFormatSupported(Exclusive) call returns %08x\n", hr);
235         hexcl = hr;
236 
237         pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
238         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
239         ok(hr == hexcl, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
240         ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
241 
242         if (hexcl != AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
243             hexcl = S_OK;
244 
245         hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
246         ok(hr == E_INVALIDARG/*w32*/ ||
247            broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
248            "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
249     }
250 
251     test_uninitialized(ac);
252 
253     hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
254     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
255 
256     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
257     ok(hr == E_INVALIDARG ||
258             hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
259 
260     /* A period != 0 is ignored and the call succeeds.
261      * Since we can only initialize successfully once, skip those tests.
262      */
263     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
264     ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
265 
266     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, pwfx, NULL);
267     ok(hr == S_OK, "Initialize with 0 buffer size returns %08x\n", hr);
268     if(hr == S_OK){
269         UINT32 num;
270 
271         hr = IAudioClient_GetBufferSize(ac, &num);
272         ok(hr == S_OK, "GetBufferSize from duration 0 returns %08x\n", hr);
273         if(hr == S_OK)
274             trace("Initialize(duration=0) GetBufferSize is %u\n", num);
275     }
276 
277     IAudioClient_Release(ac);
278 
279     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
280             NULL, (void**)&ac);
281     ok(hr == S_OK, "Activation failed with %08x\n", hr);
282 
283     if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
284         WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
285         WAVEFORMATEX *fmt2 = NULL;
286 
287         ok(fmtex->dwChannelMask != 0, "Got empty dwChannelMask\n");
288 
289         fmtex->dwChannelMask = 0xffff;
290 
291         hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
292         ok(hr == S_OK ||
293                 hr == AUDCLNT_E_UNSUPPORTED_FORMAT /* win10 */, "Initialize(dwChannelMask = 0xffff) returns %08x\n", hr);
294 
295         IAudioClient_Release(ac);
296 
297         hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
298                 NULL, (void**)&ac);
299         ok(hr == S_OK, "Activation failed with %08x\n", hr);
300 
301         fmtex->dwChannelMask = 0;
302 
303         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &fmt2);
304         ok(hr == S_OK || broken(hr == S_FALSE /* w7 Realtek HDA */),
305            "IsFormatSupported(dwChannelMask = 0) call returns %08x\n", hr);
306         ok(fmtex->dwChannelMask == 0, "Passed format was modified\n");
307 
308         CoTaskMemFree(fmt2);
309 
310         hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
311         ok(hr == S_OK, "Initialize(dwChannelMask = 0) returns %08x\n", hr);
312 
313         IAudioClient_Release(ac);
314 
315         hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
316                 NULL, (void**)&ac);
317         ok(hr == S_OK, "Activation failed with %08x\n", hr);
318 
319         CoTaskMemFree(pwfx);
320 
321         hr = IAudioClient_GetMixFormat(ac, &pwfx);
322         ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
323     }else
324         skip("Skipping dwChannelMask tests\n");
325 
326     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
327     ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
328     if (hr != S_OK)
329         goto cleanup;
330 
331     hr = IAudioClient_GetStreamLatency(ac, NULL);
332     ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
333 
334     hr = IAudioClient_GetStreamLatency(ac, &t2);
335     ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
336     trace("Returned latency: %u.%04u ms\n",
337           (UINT)(t2/10000), (UINT)(t2 % 10000));
338     ok(t2 >= t1 || broken(t2 >= t1/2 && pwfx->nSamplesPerSec > 48000) ||
339             broken(t2 == 0) /* (!) win10 */,
340        "Latency < default period, delta %dus (%s vs %s)\n",
341        (LONG)((t2-t1)/10), wine_dbgstr_longlong(t2), wine_dbgstr_longlong(t1));
342     /* Native appears to add the engine period to the HW latency in shared mode */
343     if(t2 == 0)
344         win10 = TRUE;
345 
346     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
347     ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
348 
349     hr = IAudioClient_SetEventHandle(ac, NULL);
350     ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
351 
352     hr = IAudioClient_SetEventHandle(ac, handle);
353     ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
354        broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
355        broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
356        broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
357        , "SetEventHandle returns %08x\n", hr);
358 
359     hr = IAudioClient_Reset(ac);
360     ok(hr == S_OK, "Reset on an initialized stream returns %08x\n", hr);
361 
362     hr = IAudioClient_Reset(ac);
363     ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
364 
365     hr = IAudioClient_Stop(ac);
366     ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
367 
368     hr = IAudioClient_Start(ac);
369     ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
370 
371     hr = IAudioClient_Start(ac);
372     ok(hr == AUDCLNT_E_NOT_STOPPED, "Start twice returns %08x\n", hr);
373 
374 cleanup:
375     IAudioClient_Release(ac);
376     CloseHandle(handle);
377     CoTaskMemFree(pwfx);
378 }
379 
380 static void test_formats(AUDCLNT_SHAREMODE mode)
381 {
382     IAudioClient *ac;
383     HRESULT hr, hrs;
384     WAVEFORMATEX fmt, *pwfx, *pwfx2;
385     int i;
386 
387     fmt.wFormatTag = WAVE_FORMAT_PCM;
388     fmt.cbSize = 0;
389 
390     for(i = 0; i < NB_WIN_FORMATS; i++) {
391         hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
392                 NULL, (void**)&ac);
393         ok(hr == S_OK, "Activation failed with %08x\n", hr);
394         if(hr != S_OK)
395             continue;
396 
397         hr = IAudioClient_GetMixFormat(ac, &pwfx);
398         ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
399 
400         fmt.nSamplesPerSec = win_formats[i][0];
401         fmt.wBitsPerSample = win_formats[i][1];
402         fmt.nChannels      = win_formats[i][2];
403         fmt.nBlockAlign    = fmt.nChannels * fmt.wBitsPerSample / 8;
404         fmt.nAvgBytesPerSec= fmt.nBlockAlign * fmt.nSamplesPerSec;
405 
406         pwfx2 = (WAVEFORMATEX*)0xDEADF00D;
407         hr = IAudioClient_IsFormatSupported(ac, mode, &fmt, &pwfx2);
408         hrs = hr;
409         /* Only shared mode suggests something ... GetMixFormat! */
410         ok(hr == S_OK || (mode == AUDCLNT_SHAREMODE_SHARED
411            ? hr == S_FALSE || broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT &&
412                /* 5:1 card exception when asked for 1 channel at mixer rate */
413                pwfx->nChannels > 2 && fmt.nSamplesPerSec == pwfx->nSamplesPerSec)
414            : (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == hexcl)),
415            "IsFormatSupported(%d, %ux%2ux%u) returns %08x\n", mode,
416            fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
417         if (hr == S_OK)
418             trace("IsSupported(%s, %ux%2ux%u)\n",
419                   mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
420                   fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels);
421 
422         /* Change GetMixFormat wBitsPerSample only => S_OK */
423         if (mode == AUDCLNT_SHAREMODE_SHARED
424             && fmt.nSamplesPerSec == pwfx->nSamplesPerSec
425             && fmt.nChannels == pwfx->nChannels)
426             ok(hr == S_OK, "Varying BitsPerSample %u\n", fmt.wBitsPerSample);
427 
428         ok((hr == S_FALSE)^(pwfx2 == NULL), "hr %x<->suggest %p\n", hr, pwfx2);
429         if (pwfx2 == (WAVEFORMATEX*)0xDEADF00D)
430             pwfx2 = NULL; /* broken in Wine < 1.3.28 */
431         if (pwfx2) {
432             ok(pwfx2->nSamplesPerSec == pwfx->nSamplesPerSec &&
433                pwfx2->nChannels      == pwfx->nChannels &&
434                pwfx2->wBitsPerSample == pwfx->wBitsPerSample,
435                "Suggestion %ux%2ux%u differs from GetMixFormat\n",
436                pwfx2->nSamplesPerSec, pwfx2->wBitsPerSample, pwfx2->nChannels);
437         }
438 
439         /* Vista returns E_INVALIDARG upon AUDCLNT_STREAMFLAGS_RATEADJUST */
440         hr = IAudioClient_Initialize(ac, mode, 0, 5000000, 0, &fmt, NULL);
441         if ((hrs == S_OK) ^ (hr == S_OK))
442             trace("Initialize (%s, %ux%2ux%u) returns %08x unlike IsFormatSupported\n",
443                   mode == AUDCLNT_SHAREMODE_SHARED ? "shared " : "exclus.",
444                   fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
445         if (mode == AUDCLNT_SHAREMODE_SHARED)
446             ok(hrs == S_OK ? hr == S_OK : hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
447                "Initialize(shared,  %ux%2ux%u) returns %08x\n",
448                fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
449         else if (hrs == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
450             /* Unsupported format implies "create failed" and shadows "not allowed" */
451             ok(hrs == hexcl && (hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == hrs),
452                "Initialize(noexcl., %ux%2ux%u) returns %08x(%08x)\n",
453                fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr, hrs);
454         else
455             /* On testbot 48000x16x1 claims support, but does not Initialize.
456              * Some cards Initialize 44100|48000x16x1 yet claim no support;
457              * F. Gouget's w7 bots do that for 12000|96000x8|16x1|2 */
458             ok(hrs == S_OK ? hr == S_OK || broken(hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED)
459                : hr == AUDCLNT_E_ENDPOINT_CREATE_FAILED || hr == AUDCLNT_E_UNSUPPORTED_FORMAT ||
460                  broken(hr == S_OK &&
461                      ((fmt.nChannels == 1 && fmt.wBitsPerSample == 16) ||
462                       (fmt.nSamplesPerSec == 12000 || fmt.nSamplesPerSec == 96000))),
463                "Initialize(exclus., %ux%2ux%u) returns %08x\n",
464                fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nChannels, hr);
465 
466         /* Bug in native (Vista/w2k8/w7): after Initialize failed, better
467          * Release this ac and Activate a new one.
468          * A second call (with a known working format) would yield
469          * ALREADY_INITIALIZED in shared mode yet be unusable, and in exclusive
470          * mode some entity keeps a lock on the device, causing DEVICE_IN_USE to
471          * all subsequent calls until the audio engine service is restarted. */
472 
473         CoTaskMemFree(pwfx2);
474         CoTaskMemFree(pwfx);
475         IAudioClient_Release(ac);
476     }
477 }
478 
479 static void test_references(void)
480 {
481     IAudioClient *ac;
482     IAudioRenderClient *rc;
483     ISimpleAudioVolume *sav;
484     IAudioStreamVolume *asv;
485     IAudioClock *acl;
486     WAVEFORMATEX *pwfx;
487     HRESULT hr;
488     ULONG ref;
489 
490     /* IAudioRenderClient */
491     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
492             NULL, (void**)&ac);
493     ok(hr == S_OK, "Activation failed with %08x\n", hr);
494     if(hr != S_OK)
495         return;
496 
497     hr = IAudioClient_GetMixFormat(ac, &pwfx);
498     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
499 
500     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
501             0, pwfx, NULL);
502     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
503 
504     CoTaskMemFree(pwfx);
505 
506     hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
507     ok(hr == S_OK, "GetService failed: %08x\n", hr);
508     if(hr != S_OK) {
509         IAudioClient_Release(ac);
510         return;
511     }
512 
513     IAudioRenderClient_AddRef(rc);
514     ref = IAudioRenderClient_Release(rc);
515     ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
516 
517     ref = IAudioClient_Release(ac);
518     ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
519 
520     ref = IAudioRenderClient_Release(rc);
521     ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
522 
523     /* ISimpleAudioVolume */
524     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
525             NULL, (void**)&ac);
526     ok(hr == S_OK, "Activation failed with %08x\n", hr);
527     if(hr != S_OK)
528         return;
529 
530     hr = IAudioClient_GetMixFormat(ac, &pwfx);
531     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
532 
533     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
534             0, pwfx, NULL);
535     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
536 
537     CoTaskMemFree(pwfx);
538 
539     hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
540     ok(hr == S_OK, "GetService failed: %08x\n", hr);
541 
542     ISimpleAudioVolume_AddRef(sav);
543     ref = ISimpleAudioVolume_Release(sav);
544     ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
545 
546     ref = IAudioClient_Release(ac);
547     ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
548 
549     ref = ISimpleAudioVolume_Release(sav);
550     ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
551 
552     /* IAudioClock */
553     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
554             NULL, (void**)&ac);
555     ok(hr == S_OK, "Activation failed with %08x\n", hr);
556     if(hr != S_OK)
557         return;
558 
559     hr = IAudioClient_GetMixFormat(ac, &pwfx);
560     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
561 
562     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
563             0, pwfx, NULL);
564     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
565 
566     CoTaskMemFree(pwfx);
567 
568     hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
569     ok(hr == S_OK, "GetService failed: %08x\n", hr);
570 
571     IAudioClock_AddRef(acl);
572     ref = IAudioClock_Release(acl);
573     ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
574 
575     ref = IAudioClient_Release(ac);
576     ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
577 
578     ref = IAudioClock_Release(acl);
579     ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
580 
581     /* IAudioStreamVolume */
582     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
583             NULL, (void**)&ac);
584     ok(hr == S_OK, "Activation failed with %08x\n", hr);
585     if(hr != S_OK)
586         return;
587 
588     hr = IAudioClient_GetMixFormat(ac, &pwfx);
589     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
590 
591     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
592             0, pwfx, NULL);
593     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
594 
595     CoTaskMemFree(pwfx);
596 
597     hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
598     ok(hr == S_OK, "GetService failed: %08x\n", hr);
599 
600     IAudioStreamVolume_AddRef(asv);
601     ref = IAudioStreamVolume_Release(asv);
602     ok(ref != 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
603 
604     ref = IAudioClient_Release(ac);
605     ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
606 
607     ref = IAudioStreamVolume_Release(asv);
608     ok(ref == 0, "AudioStreamVolume_Release gave wrong refcount: %u\n", ref);
609 }
610 
611 static void test_event(void)
612 {
613     HANDLE event;
614     HRESULT hr;
615     DWORD r;
616     IAudioClient *ac;
617     WAVEFORMATEX *pwfx;
618 
619     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
620             NULL, (void**)&ac);
621     ok(hr == S_OK, "Activation failed with %08x\n", hr);
622     if(hr != S_OK)
623         return;
624 
625     hr = IAudioClient_GetMixFormat(ac, &pwfx);
626     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
627 
628     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
629             AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
630             0, pwfx, NULL);
631     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
632 
633     CoTaskMemFree(pwfx);
634 
635     event = CreateEventW(NULL, FALSE, FALSE, NULL);
636     ok(event != NULL, "CreateEvent failed\n");
637 
638     hr = IAudioClient_Start(ac);
639     ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET ||
640             hr == D3D11_ERROR_4E /* win10 */, "Start failed: %08x\n", hr);
641 
642     hr = IAudioClient_SetEventHandle(ac, event);
643     ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
644 
645     hr = IAudioClient_SetEventHandle(ac, event);
646     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME) ||
647             hr == E_UNEXPECTED /* win10 */, "SetEventHandle returns %08x\n", hr);
648 
649     r = WaitForSingleObject(event, 40);
650     ok(r == WAIT_TIMEOUT, "Wait(event) before Start gave %x\n", r);
651 
652     hr = IAudioClient_Start(ac);
653     ok(hr == S_OK, "Start failed: %08x\n", hr);
654 
655     r = WaitForSingleObject(event, 20);
656     ok(r == WAIT_OBJECT_0, "Wait(event) after Start gave %x\n", r);
657 
658     hr = IAudioClient_Stop(ac);
659     ok(hr == S_OK, "Stop failed: %08x\n", hr);
660 
661     ok(ResetEvent(event), "ResetEvent\n");
662 
663     /* Still receiving events! */
664     r = WaitForSingleObject(event, 20);
665     ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
666 
667     hr = IAudioClient_Reset(ac);
668     ok(hr == S_OK, "Reset failed: %08x\n", hr);
669 
670     ok(ResetEvent(event), "ResetEvent\n");
671 
672     r = WaitForSingleObject(event, 120);
673     ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
674 
675     hr = IAudioClient_SetEventHandle(ac, NULL);
676     ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
677 
678     r = WaitForSingleObject(event, 70);
679     ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
680 
681     /* test releasing a playing stream */
682     hr = IAudioClient_Start(ac);
683     ok(hr == S_OK, "Start failed: %08x\n", hr);
684     IAudioClient_Release(ac);
685 
686     CloseHandle(event);
687 }
688 
689 static void test_padding(void)
690 {
691     HRESULT hr;
692     IAudioClient *ac;
693     IAudioRenderClient *arc;
694     WAVEFORMATEX *pwfx;
695     REFERENCE_TIME minp, defp;
696     BYTE *buf, silence;
697     UINT32 psize, pad, written, i;
698 
699     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
700             NULL, (void**)&ac);
701     ok(hr == S_OK, "Activation failed with %08x\n", hr);
702     if(hr != S_OK)
703         return;
704 
705     hr = IAudioClient_GetMixFormat(ac, &pwfx);
706     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
707     if(hr != S_OK)
708         return;
709 
710     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
711             0, 5000000, 0, pwfx, NULL);
712     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
713     if(hr != S_OK)
714         return;
715 
716     if(pwfx->wBitsPerSample == 8)
717         silence = 128;
718     else
719         silence = 0;
720 
721     /** GetDevicePeriod
722      * Default (= shared) device period is 10ms (e.g. 441 frames at 44100),
723      * except when the HW/OS forces a particular alignment,
724      * e.g. 10.1587ms is 28 * 16 = 448 frames at 44100 with HDA.
725      * 441 observed with Vista, 448 with w7 on the same HW! */
726     hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
727     ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
728     /* some wineXYZ.drv use 20ms, not seen on native */
729     ok(defp == 100000 || broken(defp == 101587) || defp == 200000,
730        "Expected 10ms default period: %u\n", (ULONG)defp);
731     ok(minp != 0, "Minimum period is 0\n");
732     ok(minp <= defp, "Minimum period is greater than default period\n");
733 
734     hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
735     ok(hr == S_OK, "GetService failed: %08x\n", hr);
736 
737     psize = MulDiv(defp, pwfx->nSamplesPerSec, 10000000) * 10;
738 
739     written = 0;
740     hr = IAudioClient_GetCurrentPadding(ac, &pad);
741     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
742     ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
743 
744     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
745     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
746     ok(buf != NULL, "NULL buffer returned\n");
747     if(!win10){
748         /* win10 appears not to clear the buffer */
749         for(i = 0; i < psize * pwfx->nBlockAlign; ++i){
750             if(buf[i] != silence){
751                 ok(0, "buffer has data in it already, i: %u, valu: %f\n", i, *((float*)buf));
752                 break;
753             }
754         }
755     }
756 
757     hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
758     ok(hr == AUDCLNT_E_OUT_OF_ORDER, "GetBuffer 0 size failed: %08x\n", hr);
759     ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
760     /* MSDN instead documents buf remains untouched */
761 
762     hr = IAudioClient_Reset(ac);
763     ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
764 
765     hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
766             AUDCLNT_BUFFERFLAGS_SILENT);
767     ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
768     if(hr == S_OK) written += psize;
769 
770     hr = IAudioClient_GetCurrentPadding(ac, &pad);
771     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
772     ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
773 
774     psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 10;
775 
776     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
777     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
778     ok(buf != NULL, "NULL buffer returned\n");
779 
780     hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
781             AUDCLNT_BUFFERFLAGS_SILENT);
782     ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
783     written += psize;
784 
785     hr = IAudioClient_GetCurrentPadding(ac, &pad);
786     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
787     ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
788 
789     /* overfull buffer. requested 1/2s buffer size, so try
790      * to get a 1/2s buffer, which should fail */
791     psize = pwfx->nSamplesPerSec / 2;
792     buf = (void*)0xDEADF00D;
793     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
794     ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
795     ok(buf == NULL, "NULL expected %p\n", buf);
796 
797     hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
798     ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
799 
800     psize = MulDiv(minp, pwfx->nSamplesPerSec, 10000000) * 2;
801 
802     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
803     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
804     ok(buf != NULL, "NULL buffer returned\n");
805 
806     hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
807     ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
808 
809     buf = (void*)0xDEADF00D;
810     hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
811     ok(hr == S_OK, "GetBuffer 0 size failed: %08x\n", hr);
812     ok(buf == NULL, "GetBuffer 0 gave %p\n", buf);
813     /* MSDN instead documents buf remains untouched */
814 
815     buf = (void*)0xDEADF00D;
816     hr = IAudioRenderClient_GetBuffer(arc, 0, &buf);
817     ok(hr == S_OK, "GetBuffer 0 size #2 failed: %08x\n", hr);
818     ok(buf == NULL, "GetBuffer 0 #2 gave %p\n", buf);
819 
820     hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
821     ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer not size 0 gave %08x\n", hr);
822 
823     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
824     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
825     ok(buf != NULL, "NULL buffer returned\n");
826 
827     hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
828     ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
829 
830     hr = IAudioClient_GetCurrentPadding(ac, &pad);
831     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
832     ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
833 
834     hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
835     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
836     ok(buf != NULL, "NULL buffer returned\n");
837 
838     hr = IAudioRenderClient_ReleaseBuffer(arc, psize+1, AUDCLNT_BUFFERFLAGS_SILENT);
839     ok(hr == AUDCLNT_E_INVALID_SIZE, "ReleaseBuffer too large error: %08x\n", hr);
840     /* todo_wine means Wine may overwrite memory */
841     if(hr == S_OK) written += psize+1;
842 
843     /* Buffer still hold */
844     hr = IAudioRenderClient_ReleaseBuffer(arc, psize/2, AUDCLNT_BUFFERFLAGS_SILENT);
845     ok(hr == S_OK, "ReleaseBuffer after error: %08x\n", hr);
846     if(hr == S_OK) written += psize/2;
847 
848     hr = IAudioRenderClient_ReleaseBuffer(arc, 0, 0);
849     ok(hr == S_OK, "ReleaseBuffer 0 gave wrong error: %08x\n", hr);
850 
851     hr = IAudioClient_GetCurrentPadding(ac, &pad);
852     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
853     ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
854 
855     CoTaskMemFree(pwfx);
856 
857     IAudioRenderClient_Release(arc);
858     IAudioClient_Release(ac);
859 }
860 
861 static void test_clock(int share)
862 {
863     HRESULT hr;
864     IAudioClient *ac;
865     IAudioClock *acl;
866     IAudioRenderClient *arc;
867     UINT64 freq, pos, pcpos0, pcpos, last;
868     UINT32 pad, gbsize, bufsize, fragment, parts, avail, slept = 0, sum = 0;
869     BYTE *data;
870     WAVEFORMATEX *pwfx;
871     LARGE_INTEGER hpctime, hpctime0, hpcfreq;
872     REFERENCE_TIME minp, defp, t1, t2;
873     REFERENCE_TIME duration = 5000000, period = 150000;
874     int i;
875 
876     ok(QueryPerformanceFrequency(&hpcfreq), "PerfFrequency failed\n");
877 
878     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
879             NULL, (void**)&ac);
880     ok(hr == S_OK, "Activation failed with %08x\n", hr);
881     if(hr != S_OK)
882         return;
883 
884     hr = IAudioClient_GetMixFormat(ac, &pwfx);
885     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
886     if(hr != S_OK)
887         return;
888 
889     hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
890     ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
891     ok(minp <= period, "desired period %u too small for %u\n", (ULONG)period, (ULONG)minp);
892 
893     if (share) {
894         trace("Testing shared mode\n");
895         /* period is ignored */
896         hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
897                 0, duration, period, pwfx, NULL);
898         period = defp;
899     } else {
900         pwfx->wFormatTag = WAVE_FORMAT_PCM;
901         pwfx->nChannels = 2;
902         pwfx->cbSize = 0;
903         pwfx->wBitsPerSample = 16; /* no floating point */
904         pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
905         pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
906         trace("Testing exclusive mode at %u\n", pwfx->nSamplesPerSec);
907 
908         hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_EXCLUSIVE,
909                 0, duration, period, pwfx, NULL);
910     }
911     ok(share ? hr == S_OK : hr == hexcl || hr == AUDCLNT_E_DEVICE_IN_USE, "Initialize failed: %08x\n", hr);
912     if (hr != S_OK) {
913         CoTaskMemFree(pwfx);
914         IAudioClient_Release(ac);
915         if(hr == AUDCLNT_E_DEVICE_IN_USE)
916             skip("Device in use, no %s access\n", share ? "shared" : "exclusive");
917         return;
918     }
919 
920     /** GetStreamLatency
921      * Shared mode: 1x period + a little, but some 192000 devices return 5.3334ms.
922      * Exclusive mode: testbot returns 2x period + a little, but
923      * some HDA drivers return 1x period, some + a little. */
924     hr = IAudioClient_GetStreamLatency(ac, &t2);
925     ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
926     trace("Latency: %u.%04u ms\n", (UINT)(t2/10000), (UINT)(t2 % 10000));
927     ok(t2 >= period || broken(t2 >= period/2 && share && pwfx->nSamplesPerSec > 48000) ||
928             broken(t2 == 0) /* win10 */,
929        "Latency < default period, delta %ldus\n", (long)((t2-period)/10));
930 
931     /** GetBufferSize
932      * BufferSize must be rounded up, maximum 2s says MSDN.
933      * Both is wrong.  Rounding may lead to size a little smaller than duration;
934      * duration > 2s is accepted in shared mode.
935      * Shared mode: round solely w.r.t. mixer rate,
936      *              duration is no multiple of period.
937      * Exclusive mode: size appears as a multiple of some fragment that
938      * is either the rounded period or a fixed constant like 1024,
939      * whatever the driver implements. */
940     hr = IAudioClient_GetBufferSize(ac, &gbsize);
941     ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
942 
943     bufsize   =  MulDiv(duration, pwfx->nSamplesPerSec, 10000000);
944     fragment  =  MulDiv(period,   pwfx->nSamplesPerSec, 10000000);
945     parts     =  MulDiv(bufsize, 1, fragment); /* instead of (duration, 1, period) */
946     trace("BufferSize %u estimated fragment %u x %u = %u\n", gbsize, fragment, parts, fragment * parts);
947     /* fragment size (= period in frames) is rounded up.
948      * BufferSize must be rounded up, maximum 2s says MSDN
949      * but it is rounded down modulo fragment ! */
950     if (share)
951     ok(gbsize == bufsize,
952        "BufferSize %u at rate %u\n", gbsize, pwfx->nSamplesPerSec);
953     else
954         todo_wine
955         ok(gbsize == parts * fragment || gbsize == MulDiv(bufsize, 1, 1024) * 1024,
956            "BufferSize %u misfits fragment size %u at rate %u\n", gbsize, fragment, pwfx->nSamplesPerSec);
957 
958     /* In shared mode, GetCurrentPadding decreases in multiples of
959      * fragment size (i.e. updated only at period ticks), whereas
960      * GetPosition appears to be reporting continuous positions.
961      * In exclusive mode, testbot behaves likewise, but native's Intel
962      * HDA driver shows no such deltas, GetCurrentPadding closely
963      * matches GetPosition, as in
964      * GetCurrentPadding = GetPosition - frames held in mmdevapi */
965 
966     hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
967     ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
968 
969     hr = IAudioClock_GetFrequency(acl, &freq);
970     ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
971     trace("Clock Frequency %u\n", (UINT)freq);
972 
973     /* MSDN says it's arbitrary units, but shared mode is unlikely to change */
974     if (share)
975         ok(freq == pwfx->nSamplesPerSec * pwfx->nBlockAlign,
976            "Clock Frequency %u\n", (UINT)freq);
977     else
978         ok(freq == pwfx->nSamplesPerSec,
979            "Clock Frequency %u\n", (UINT)freq);
980 
981     hr = IAudioClock_GetPosition(acl, NULL, NULL);
982     ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr);
983 
984     pcpos0 = 0;
985     hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
986     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
987     ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
988     ok(pcpos0 != 0, "GetPosition returned zero pcpos\n");
989 
990     hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
991     ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
992 
993     hr = IAudioRenderClient_GetBuffer(arc, gbsize+1, &data);
994     ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer too large failed: %08x\n", hr);
995 
996     avail = gbsize;
997     data = NULL;
998     hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
999     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1000     trace("data at %p\n", data);
1001 
1002     hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1003         wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1004     ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1005     if(hr == S_OK) sum += avail;
1006 
1007     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1008     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1009     ok(pad == sum, "padding %u prior to start\n", pad);
1010 
1011     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1012     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1013     ok(pos == 0, "GetPosition returned non-zero pos before being started\n");
1014 
1015     hr = IAudioClient_Start(ac); /* #1 */
1016     ok(hr == S_OK, "Start failed: %08x\n", hr);
1017 
1018     Sleep(100);
1019     slept += 100;
1020 
1021     hr = IAudioClient_GetStreamLatency(ac, &t1);
1022     ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1023     ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1024 
1025     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1026     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1027     ok(pos > 0, "Position %u vs. last %u\n", (UINT)pos,0);
1028     /* in rare cases is slept*1.1 not enough with dmix */
1029     ok(pos*1000/freq <= slept*1.4, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1030     last = pos;
1031 
1032     hr = IAudioClient_Stop(ac);
1033     ok(hr == S_OK, "Stop failed: %08x\n", hr);
1034 
1035     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1036     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1037     ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1038     last = pos;
1039     if(/*share &&*/ winetest_debug>1)
1040         ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1041 
1042     hr = IAudioClient_Start(ac); /* #2 */
1043     ok(hr == S_OK, "Start failed: %08x\n", hr);
1044 
1045     Sleep(100);
1046     slept += 100;
1047 
1048     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1049     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1050     trace("padding %u past sleep #2\n", pad);
1051 
1052     /** IAudioClient_Stop
1053      * Exclusive mode: the audio engine appears to drop frames,
1054      * bumping GetPosition to a higher value than time allows, even
1055      * allowing GetPosition > sum Released - GetCurrentPadding (testbot)
1056      * Shared mode: no drop observed (or too small to be visible).
1057      * GetPosition = sum Released - GetCurrentPadding
1058      * Bugs: Some USB headset system drained the whole buffer, leaving
1059      *       padding 0 and bumping pos to sum minus 17 frames! */
1060 
1061     hr = IAudioClient_Stop(ac);
1062     ok(hr == S_OK, "Stop failed: %08x\n", hr);
1063 
1064     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1065     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1066 
1067     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1068     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1069     trace("padding %u position %u past stop #2\n", pad, (UINT)pos);
1070     ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1071     /* Prove that Stop must not drop frames (in shared mode). */
1072     ok(pad ? pos > last : pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1073     if (share && pad > 0 && winetest_debug>1)
1074         ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1075     /* in exclusive mode, testbot's w7 machines yield pos > sum-pad */
1076     if(/*share &&*/ winetest_debug>1)
1077         ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1078            "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1079     last = pos;
1080 
1081     Sleep(100);
1082 
1083     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1084     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1085     ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1086 
1087     /* Restart from 0 */
1088     hr = IAudioClient_Reset(ac);
1089     ok(hr == S_OK, "Reset failed: %08x\n", hr);
1090     slept = sum = 0;
1091 
1092     hr = IAudioClient_Reset(ac);
1093     ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
1094 
1095     hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1096     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1097     ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1098     ok(pcpos > pcpos0, "pcpos should increase\n");
1099 
1100     avail = gbsize; /* implies GetCurrentPadding == 0 */
1101     hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1102     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1103     trace("data at %p\n", data);
1104 
1105     hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1106         wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1107     ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1108     if(hr == S_OK) sum += avail;
1109 
1110     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1111     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1112     ok(pad == sum, "padding %u prior to start\n", pad);
1113 
1114     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1115     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1116     ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1117     last = pos;
1118 
1119     hr = IAudioClient_Start(ac); /* #3 */
1120     ok(hr == S_OK, "Start failed: %08x\n", hr);
1121 
1122     Sleep(100);
1123     slept += 100;
1124 
1125     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1126     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1127     trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1128     ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1129     ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1130     if (winetest_debug>1)
1131         ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1132     else
1133         skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1134     last = pos;
1135 
1136     hr = IAudioClient_Reset(ac);
1137     ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1138 
1139     hr = IAudioClient_Stop(ac);
1140     ok(hr == S_OK, "Stop failed: %08x\n", hr);
1141 
1142     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1143     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1144 
1145     hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1146     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1147     trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1148     ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1149     ok(pcpos > pcpos0, "pcpos should increase\n");
1150     ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1151     if (pad > 0 && winetest_debug>1)
1152         ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1153     if(winetest_debug>1)
1154         ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1155            "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1156     last = pos;
1157 
1158     /* Begin the big loop */
1159     hr = IAudioClient_Reset(ac);
1160     ok(hr == S_OK, "Reset failed: %08x\n", hr);
1161     slept = last = sum = 0;
1162     pcpos0 = pcpos;
1163 
1164     ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1165 
1166     hr = IAudioClient_Reset(ac);
1167     ok(hr == S_OK, "Reset on an already reset stream returns %08x\n", hr);
1168 
1169     hr = IAudioClient_Start(ac);
1170     ok(hr == S_OK, "Start failed: %08x\n", hr);
1171 
1172     avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1173     data = NULL;
1174     hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1175     ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1176     trace("data at %p for prefill %u\n", data, avail);
1177 
1178     if (winetest_debug>2) {
1179         hr = IAudioClient_Stop(ac);
1180         ok(hr == S_OK, "Stop failed: %08x\n", hr);
1181 
1182         Sleep(20);
1183         slept += 20;
1184 
1185         hr = IAudioClient_Reset(ac);
1186         ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1187 
1188         hr = IAudioClient_Start(ac);
1189         ok(hr == S_OK, "Start failed: %08x\n", hr);
1190     }
1191 
1192     /* Despite passed time, data must still point to valid memory... */
1193     hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1194         wave_generate_tone(pwfx, data, avail));
1195     ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1196     if(hr == S_OK) sum += avail;
1197 
1198     /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1199      * mixer may still have a little data.  We believe an underrun will occur
1200      * when the mixer finds GCP smaller than a period size at the *end* of a
1201      * period cycle, i.e. shortly before calling SetEvent to signal the app
1202      * that it has ~10ms to supply data for the next cycle.  IOW, a zero GCP
1203      * with no data written for over a period causes an underrun. */
1204 
1205     Sleep(350);
1206     slept += 350;
1207     ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1208     trace("hpctime %u after %ums\n",
1209         (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1210 
1211     hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1212     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1213     ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1214     last = pos;
1215 
1216     for(i=0; i < 9; i++) {
1217         Sleep(100);
1218         slept += 100;
1219 
1220         hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1221         ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1222 
1223         hr = IAudioClient_GetCurrentPadding(ac, &pad);
1224         ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1225 
1226         ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1227         trace("hpctime %u pcpos %u\n",
1228               (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1229               (ULONG)((pcpos-pcpos0)/10000));
1230 
1231         /* Use sum-pad to see whether position is ahead padding or not. */
1232         trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1233         ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1234         ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1235         if (winetest_debug>1) {
1236             /* Padding does not lag behind by much */
1237             ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1238             ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1239             if (pad) /* not in case of underrun */
1240                 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1241                    "Position delta %ld not regular: %ld ms\n", (long)(pos-last), (long)((pos-last)*1000/freq));
1242         }
1243         last = pos;
1244 
1245         hr = IAudioClient_GetStreamLatency(ac, &t1);
1246         ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1247         ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1248 
1249         avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1250         data = NULL;
1251         hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1252         /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1253         ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1254            "GetBuffer large (%u) failed: %08x\n", avail, hr);
1255         if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1256         /* Only the first iteration should allow that large a buffer
1257          * as prefill was drained during the first 350+100ms sleep.
1258          * Afterwards, only 100ms of data should find room per iteration. */
1259 
1260         if(hr == S_OK) {
1261             trace("data at %p\n", data);
1262         } else {
1263             avail = gbsize - pad;
1264             hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1265             ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1266             trace("data at %p (small %u)\n", data, avail);
1267         }
1268         ok(data != NULL, "NULL buffer returned\n");
1269         if(i % 3 && !winetest_interactive) {
1270             memset(data, 0, avail * pwfx->nBlockAlign);
1271             hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1272         } else {
1273             hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1274                 wave_generate_tone(pwfx, data, avail));
1275         }
1276         ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1277         if(hr == S_OK) sum += avail;
1278     }
1279 
1280     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1281     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1282     trace("position %u\n", (UINT)pos);
1283 
1284     Sleep(1000); /* 500ms buffer underrun past full buffer */
1285 
1286     hr = IAudioClient_GetCurrentPadding(ac, &pad);
1287     ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1288 
1289     hr = IAudioClock_GetPosition(acl, &pos, NULL);
1290     ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1291     trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1292 
1293     if (share) {
1294         /* Following underrun, all samples were played */
1295         ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1296         ok(pos * pwfx->nSamplesPerSec == sum * freq,
1297            "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1298     } else {
1299         /* Vista and w2k8 leave partial fragments behind */
1300         ok(pad == 0 /* w7, w2k8R2 */||
1301            pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1302         /* expect at most 5 fragments (75ms) away */
1303         ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1304            pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1305            "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1306     }
1307 
1308     hr = IAudioClient_GetStreamLatency(ac, &t1);
1309     ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1310     ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1311 
1312     ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1313     trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1314 
1315     hr = IAudioClient_Stop(ac);
1316     ok(hr == S_OK, "Stop failed: %08x\n", hr);
1317 
1318     CoTaskMemFree(pwfx);
1319 
1320     IAudioClock_Release(acl);
1321     IAudioRenderClient_Release(arc);
1322     IAudioClient_Release(ac);
1323 }
1324 
1325 static void test_session(void)
1326 {
1327     IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1328     IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1329     IMMDevice *cap_dev;
1330     GUID ses1_guid;
1331     AudioSessionState state;
1332     WAVEFORMATEX *pwfx;
1333     ULONG ref;
1334     HRESULT hr;
1335 
1336     hr = CoCreateGuid(&ses1_guid);
1337     ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1338 
1339     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1340             NULL, (void**)&ses1_ac1);
1341     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1342     if (FAILED(hr)) return;
1343 
1344     hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1345     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1346 
1347     hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1348             0, 5000000, 0, pwfx, &ses1_guid);
1349     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1350 
1351     if(hr == S_OK){
1352         hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1353                 NULL, (void**)&ses1_ac2);
1354         ok(hr == S_OK, "Activation failed with %08x\n", hr);
1355     }
1356     if(hr != S_OK){
1357         skip("Unable to open the same device twice. Skipping session tests\n");
1358 
1359         ref = IAudioClient_Release(ses1_ac1);
1360         ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1361         CoTaskMemFree(pwfx);
1362         return;
1363     }
1364 
1365     hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1366             0, 5000000, 0, pwfx, &ses1_guid);
1367     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1368 
1369     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1370             eMultimedia, &cap_dev);
1371     if(hr == S_OK){
1372         hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1373                 NULL, (void**)&cap_ac);
1374         ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1375         ok(hr == S_OK, "Activate failed: %08x\n", hr);
1376         IMMDevice_Release(cap_dev);
1377     }
1378     if(hr == S_OK){
1379         WAVEFORMATEX *cap_pwfx;
1380 
1381         hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1382         ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1383 
1384         hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1385                 0, 5000000, 0, cap_pwfx, &ses1_guid);
1386         ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1387         CoTaskMemFree(cap_pwfx);
1388     }
1389     if(hr == S_OK){
1390         hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1391         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1392         if(FAILED(hr))
1393             cap_ctl = NULL;
1394     }else
1395         skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1396 
1397     hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1398     ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1399 
1400     hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1401     ok(hr == S_OK, "GetService failed: %08x\n", hr);
1402 
1403     hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1404     ok(hr == S_OK, "GetService failed: %08x\n", hr);
1405     ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1406     ref = IAudioSessionControl2_Release(ses1_ctl2);
1407     ok(ref != 0, "AudioSessionControl was destroyed\n");
1408 
1409     hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1410     ok(hr == S_OK, "GetService failed: %08x\n", hr);
1411 
1412     hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1413     ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1414 
1415     hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1416     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1417     ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1418 
1419     hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1420     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1421     ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1422 
1423     if(cap_ctl){
1424         hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1425         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1426         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1427     }
1428 
1429     hr = IAudioClient_Start(ses1_ac1);
1430     ok(hr == S_OK, "Start failed: %08x\n", hr);
1431 
1432     hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1433     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1434     ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1435 
1436     hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1437     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1438     ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1439 
1440     if(cap_ctl){
1441         hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1442         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1443         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1444     }
1445 
1446     hr = IAudioClient_Stop(ses1_ac1);
1447     ok(hr == S_OK, "Start failed: %08x\n", hr);
1448 
1449     hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1450     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1451     ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1452 
1453     hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1454     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1455     ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1456 
1457     if(cap_ctl){
1458         hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1459         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1460         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1461 
1462         hr = IAudioClient_Start(cap_ac);
1463         ok(hr == S_OK, "Start failed: %08x\n", hr);
1464 
1465         hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1466         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1467         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1468 
1469         hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1470         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1471         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1472 
1473         hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1474         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1475         ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1476 
1477         hr = IAudioClient_Stop(cap_ac);
1478         ok(hr == S_OK, "Stop failed: %08x\n", hr);
1479 
1480         hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1481         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1482         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1483 
1484         hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1485         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1486         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1487 
1488         hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1489         ok(hr == S_OK, "GetState failed: %08x\n", hr);
1490         ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1491 
1492         ref = IAudioSessionControl2_Release(cap_ctl);
1493         ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1494 
1495         ref = IAudioClient_Release(cap_ac);
1496         ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1497     }
1498 
1499     ref = IAudioSessionControl2_Release(ses1_ctl);
1500     ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1501 
1502     ref = IAudioClient_Release(ses1_ac1);
1503     ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1504 
1505     ref = IAudioClient_Release(ses1_ac2);
1506     ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1507 
1508     /* we've released all of our IAudioClient references, so check GetState */
1509     hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1510     ok(hr == S_OK, "GetState failed: %08x\n", hr);
1511     ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1512 
1513     ref = IAudioSessionControl2_Release(ses1_ctl2);
1514     ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1515 
1516     CoTaskMemFree(pwfx);
1517 }
1518 
1519 static void test_streamvolume(void)
1520 {
1521     IAudioClient *ac;
1522     IAudioStreamVolume *asv;
1523     WAVEFORMATEX *fmt;
1524     UINT32 chans, i;
1525     HRESULT hr;
1526     float vol, *vols;
1527 
1528     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1529             NULL, (void**)&ac);
1530     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1531     if(hr != S_OK)
1532         return;
1533 
1534     hr = IAudioClient_GetMixFormat(ac, &fmt);
1535     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1536 
1537     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1538             0, fmt, NULL);
1539     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1540 
1541     if(hr == S_OK){
1542         hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1543         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1544     }
1545     if(hr != S_OK){
1546         IAudioClient_Release(ac);
1547         CoTaskMemFree(fmt);
1548         return;
1549     }
1550 
1551     hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1552     ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1553 
1554     hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1555     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1556     ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1557 
1558     hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1559     ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1560 
1561     hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1562     ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1563 
1564     hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1565     ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1566 
1567     hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1568     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1569     ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1570 
1571     hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1572     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1573 
1574     hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1575     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1576 
1577     hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1578     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1579 
1580     hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1581     ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1582 
1583     hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1584     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1585     ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1586 
1587     hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1588     ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1589 
1590     hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1591     ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1592 
1593     vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1594     ok(vols != NULL, "HeapAlloc failed\n");
1595 
1596     hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1597     ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1598 
1599     hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1600     ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1601     ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1602     for(i = 1; i < fmt->nChannels; ++i)
1603         ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1604 
1605     hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1606     ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1607 
1608     hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1609     ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1610 
1611     hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1612     ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1613 
1614     hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1615     ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1616 
1617     HeapFree(GetProcessHeap(), 0, vols);
1618     IAudioStreamVolume_Release(asv);
1619     IAudioClient_Release(ac);
1620     CoTaskMemFree(fmt);
1621 }
1622 
1623 static void test_channelvolume(void)
1624 {
1625     IAudioClient *ac;
1626     IChannelAudioVolume *acv;
1627     WAVEFORMATEX *fmt;
1628     UINT32 chans, i;
1629     HRESULT hr;
1630     float vol, *vols;
1631 
1632     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1633             NULL, (void**)&ac);
1634     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1635     if(hr != S_OK)
1636         return;
1637 
1638     hr = IAudioClient_GetMixFormat(ac, &fmt);
1639     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1640 
1641     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1642             AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1643     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1644 
1645     if(hr == S_OK){
1646         hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1647         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1648     }
1649     if(hr != S_OK){
1650         IAudioClient_Release(ac);
1651         CoTaskMemFree(fmt);
1652         return;
1653     }
1654 
1655     hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1656     ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1657 
1658     hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1659     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1660     ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1661 
1662     hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1663     ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1664 
1665     hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1666     ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1667 
1668     hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1669     ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1670 
1671     hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1672     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1673     ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1674 
1675     hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1676     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1677 
1678     hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1679     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1680 
1681     hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1682     ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1683 
1684     hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1685     ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1686 
1687     hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1688     ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1689     ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1690 
1691     hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1692     ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1693 
1694     hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1695     ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1696 
1697     vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1698     ok(vols != NULL, "HeapAlloc failed\n");
1699 
1700     hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1701     ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1702 
1703     hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1704     ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1705     ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1706     for(i = 1; i < fmt->nChannels; ++i)
1707         ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1708 
1709     hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1710     ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1711 
1712     hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1713     ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1714 
1715     hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1716     ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1717 
1718     hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1719     ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1720 
1721     hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1722     ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1723 
1724     HeapFree(GetProcessHeap(), 0, vols);
1725     IChannelAudioVolume_Release(acv);
1726     IAudioClient_Release(ac);
1727     CoTaskMemFree(fmt);
1728 }
1729 
1730 static void test_simplevolume(void)
1731 {
1732     IAudioClient *ac;
1733     ISimpleAudioVolume *sav;
1734     WAVEFORMATEX *fmt;
1735     HRESULT hr;
1736     float vol;
1737     BOOL mute;
1738 
1739     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1740             NULL, (void**)&ac);
1741     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1742     if(hr != S_OK)
1743         return;
1744 
1745     hr = IAudioClient_GetMixFormat(ac, &fmt);
1746     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1747 
1748     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1749             AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1750     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1751 
1752     if(hr == S_OK){
1753         hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1754         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1755     }
1756     if(hr != S_OK){
1757         IAudioClient_Release(ac);
1758         CoTaskMemFree(fmt);
1759         return;
1760     }
1761 
1762     hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1763     ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1764 
1765     hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1766     ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1767     ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1768 
1769     hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1770     ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1771 
1772     hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1773     ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1774 
1775     hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1776     ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1777 
1778     hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1779     ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1780     ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1781 
1782     hr = ISimpleAudioVolume_GetMute(sav, NULL);
1783     ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1784 
1785     mute = TRUE;
1786     hr = ISimpleAudioVolume_GetMute(sav, &mute);
1787     ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1788     ok(mute == FALSE, "Session is already muted\n");
1789 
1790     hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1791     ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1792 
1793     mute = FALSE;
1794     hr = ISimpleAudioVolume_GetMute(sav, &mute);
1795     ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1796     ok(mute == TRUE, "Session should have been muted\n");
1797 
1798     hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1799     ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1800     ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1801 
1802     hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1803     ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1804 
1805     mute = FALSE;
1806     hr = ISimpleAudioVolume_GetMute(sav, &mute);
1807     ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1808     ok(mute == TRUE, "Session should have been muted\n");
1809 
1810     hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1811     ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1812 
1813     ISimpleAudioVolume_Release(sav);
1814     IAudioClient_Release(ac);
1815     CoTaskMemFree(fmt);
1816 }
1817 
1818 static void test_volume_dependence(void)
1819 {
1820     IAudioClient *ac, *ac2;
1821     ISimpleAudioVolume *sav;
1822     IChannelAudioVolume *cav;
1823     IAudioStreamVolume *asv;
1824     WAVEFORMATEX *fmt;
1825     HRESULT hr;
1826     float vol;
1827     GUID session;
1828     UINT32 nch;
1829 
1830     hr = CoCreateGuid(&session);
1831     ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1832 
1833     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1834             NULL, (void**)&ac);
1835     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1836     if(hr != S_OK)
1837         return;
1838 
1839     hr = IAudioClient_GetMixFormat(ac, &fmt);
1840     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1841 
1842     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1843             AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1844     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1845 
1846     if(hr == S_OK){
1847         hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1848         ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1849     }
1850     if(hr != S_OK){
1851         IAudioClient_Release(ac);
1852         CoTaskMemFree(fmt);
1853         return;
1854     }
1855 
1856     hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
1857     ok(hr == S_OK, "GetService (ChannelAudioVolume) failed: %08x\n", hr);
1858 
1859     hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1860     ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
1861 
1862     hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1863     ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
1864 
1865     hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
1866     ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1867 
1868     hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1869     ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1870 
1871     hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1872     ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1873     ok(fabsf(vol - 0.2f) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1874 
1875     hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
1876     ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1877     ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1878 
1879     hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1880     ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
1881     ok(fabsf(vol - 0.6f) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
1882 
1883     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1884             NULL, (void**)&ac2);
1885     ok(hr == S_OK, "Activation failed with %08x\n", hr);
1886 
1887     if(hr == S_OK){
1888         hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
1889                 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1890         ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1891         if(hr != S_OK)
1892             IAudioClient_Release(ac2);
1893     }
1894 
1895     if(hr == S_OK){
1896         IChannelAudioVolume *cav2;
1897         IAudioStreamVolume *asv2;
1898 
1899         hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
1900         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1901 
1902         hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
1903         ok(hr == S_OK, "GetService failed: %08x\n", hr);
1904 
1905         hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
1906         ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
1907         ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
1908 
1909         hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
1910         ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
1911         ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
1912 
1913         hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
1914         ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1915         ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1916 
1917         hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
1918         ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1919         ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
1920 
1921         IAudioStreamVolume_Release(asv2);
1922         IChannelAudioVolume_Release(cav2);
1923         IAudioClient_Release(ac2);
1924     }else
1925         skip("Unable to open the same device twice. Skipping session volume control tests\n");
1926 
1927     hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1928     ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1929 
1930     hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1931     ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1932 
1933     CoTaskMemFree(fmt);
1934     ISimpleAudioVolume_Release(sav);
1935     IChannelAudioVolume_Release(cav);
1936     IAudioStreamVolume_Release(asv);
1937     IAudioClient_Release(ac);
1938 }
1939 
1940 static void test_session_creation(void)
1941 {
1942     IMMDevice *cap_dev;
1943     IAudioClient *ac;
1944     IAudioSessionManager *sesm;
1945     ISimpleAudioVolume *sav;
1946     GUID session_guid;
1947     float vol;
1948     HRESULT hr;
1949     WAVEFORMATEX *fmt;
1950 
1951     CoCreateGuid(&session_guid);
1952 
1953     hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
1954             CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
1955     ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
1956     ok(hr == S_OK, "Activate failed: %08x\n", hr);
1957 
1958     hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
1959             FALSE, &sav);
1960     ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1961 
1962     hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
1963     ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1964 
1965     /* Release completely to show session persistence */
1966     ISimpleAudioVolume_Release(sav);
1967     IAudioSessionManager_Release(sesm);
1968 
1969     /* test if we can create a capture audioclient in the session we just
1970      * created from a SessionManager derived from a render device */
1971     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1972             eMultimedia, &cap_dev);
1973     if(hr == S_OK){
1974         WAVEFORMATEX *cap_pwfx;
1975         IAudioClient *cap_ac;
1976         ISimpleAudioVolume *cap_sav;
1977         IAudioSessionManager *cap_sesm;
1978 
1979         hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
1980                 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
1981         ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
1982         ok(hr == S_OK, "Activate failed: %08x\n", hr);
1983 
1984         hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
1985                 FALSE, &cap_sav);
1986         ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
1987 
1988         vol = 0.5f;
1989         hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
1990         ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1991 
1992         ISimpleAudioVolume_Release(cap_sav);
1993         IAudioSessionManager_Release(cap_sesm);
1994 
1995         hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
1996                 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
1997         ok(hr == S_OK, "Activate failed: %08x\n", hr);
1998 
1999         IMMDevice_Release(cap_dev);
2000 
2001         hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
2002         ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2003 
2004         hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
2005                 0, 5000000, 0, cap_pwfx, &session_guid);
2006         ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2007 
2008         CoTaskMemFree(cap_pwfx);
2009 
2010         if(hr == S_OK){
2011             hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
2012                     (void**)&cap_sav);
2013             ok(hr == S_OK, "GetService failed: %08x\n", hr);
2014         }
2015         if(hr == S_OK){
2016             vol = 0.5f;
2017             hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2018             ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2019 
2020             ISimpleAudioVolume_Release(cap_sav);
2021         }
2022 
2023         IAudioClient_Release(cap_ac);
2024     }
2025 
2026     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2027             NULL, (void**)&ac);
2028     ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2029     ok(hr == S_OK, "Activation failed with %08x\n", hr);
2030     if(hr != S_OK)
2031         return;
2032 
2033     hr = IAudioClient_GetMixFormat(ac, &fmt);
2034     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2035 
2036     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2037             AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2038     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2039 
2040     hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2041     ok(hr == S_OK, "GetService failed: %08x\n", hr);
2042     if(hr == S_OK){
2043         vol = 0.5f;
2044         hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2045         ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2046         ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2047 
2048         ISimpleAudioVolume_Release(sav);
2049     }
2050 
2051     CoTaskMemFree(fmt);
2052     IAudioClient_Release(ac);
2053 }
2054 
2055 static void test_worst_case(void)
2056 {
2057     HANDLE event;
2058     HRESULT hr;
2059     IAudioClient *ac;
2060     IAudioRenderClient *arc;
2061     IAudioClock *acl;
2062     WAVEFORMATEX *pwfx;
2063     REFERENCE_TIME defp;
2064     UINT64 freq, pos, pcpos0, pcpos;
2065     BYTE *data;
2066     DWORD r;
2067     UINT32 pad, fragment, sum;
2068     int i,j;
2069 
2070     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2071             NULL, (void**)&ac);
2072     ok(hr == S_OK, "Activation failed with %08x\n", hr);
2073     if(hr != S_OK)
2074         return;
2075 
2076     hr = IAudioClient_GetMixFormat(ac, &pwfx);
2077     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2078 
2079     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2080             AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2081     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2082     if(hr != S_OK)
2083         return;
2084 
2085     hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2086     ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2087 
2088     fragment  =  MulDiv(defp,   pwfx->nSamplesPerSec, 10000000);
2089 
2090     event = CreateEventW(NULL, FALSE, FALSE, NULL);
2091     ok(event != NULL, "CreateEvent failed\n");
2092 
2093     hr = IAudioClient_SetEventHandle(ac, event);
2094     ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2095 
2096     hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2097     ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2098 
2099     hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2100     ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2101 
2102     hr = IAudioClock_GetFrequency(acl, &freq);
2103     ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2104 
2105     for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2106         sum = 0;
2107         trace("Should play %ums continuous tone with fragment size %u.\n",
2108               (ULONG)(defp/100), fragment);
2109 
2110         hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2111         ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2112 
2113         /* XAudio2 prefills one period, play without it */
2114         if(winetest_debug>2){
2115             hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2116             ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2117 
2118             hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2119             ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2120             if(hr == S_OK)
2121                 sum += fragment;
2122         }
2123 
2124         hr = IAudioClient_Start(ac);
2125         ok(hr == S_OK, "Start failed: %08x\n", hr);
2126 
2127         for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2128             r = WaitForSingleObject(event, 60 + defp / 10000);
2129             ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2130 
2131             /* the app has nearly one period time to feed data */
2132             Sleep((i % 10) * defp / 120000);
2133 
2134             hr = IAudioClient_GetCurrentPadding(ac, &pad);
2135             ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2136 
2137             /* XAudio2 writes only when there's little data left */
2138             if(pad <= fragment){
2139                 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2140                 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2141 
2142                 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2143                        wave_generate_tone(pwfx, data, fragment));
2144                 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2145                 if(hr == S_OK)
2146                     sum += fragment;
2147             }
2148         }
2149 
2150         hr = IAudioClient_Stop(ac);
2151         ok(hr == S_OK, "Stop failed: %08x\n", hr);
2152 
2153         hr = IAudioClient_GetCurrentPadding(ac, &pad);
2154         ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2155 
2156         hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2157         ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2158 
2159         Sleep(100);
2160 
2161         trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2162               sum, sum/fragment, fragment, pad,
2163               pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2164               (ULONG)((pcpos-pcpos0)/10000));
2165 
2166         ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2167            "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2168 
2169         hr = IAudioClient_Reset(ac);
2170         ok(hr == S_OK, "Reset failed: %08x\n", hr);
2171 
2172         Sleep(250);
2173     }
2174 
2175     CoTaskMemFree(pwfx);
2176     IAudioClient_Release(ac);
2177     IAudioClock_Release(acl);
2178     IAudioRenderClient_Release(arc);
2179 }
2180 
2181 static void test_marshal(void)
2182 {
2183     IStream *pStream;
2184     IAudioClient *ac, *acDest;
2185     IAudioRenderClient *rc, *rcDest;
2186     WAVEFORMATEX *pwfx;
2187     HRESULT hr;
2188 
2189     /* IAudioRenderClient */
2190     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2191             NULL, (void**)&ac);
2192     ok(hr == S_OK, "Activation failed with %08x\n", hr);
2193     if(hr != S_OK)
2194         return;
2195 
2196     hr = IAudioClient_GetMixFormat(ac, &pwfx);
2197     ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2198 
2199     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2200             0, pwfx, NULL);
2201     ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2202 
2203     CoTaskMemFree(pwfx);
2204 
2205     hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2206     ok(hr == S_OK, "GetService failed: %08x\n", hr);
2207     if(hr != S_OK) {
2208         IAudioClient_Release(ac);
2209         return;
2210     }
2211 
2212     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2213     ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2214 
2215     /* marshal IAudioClient */
2216 
2217     hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2218     ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2219 
2220     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2221     hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2222     ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2223     if (hr == S_OK)
2224         IAudioClient_Release(acDest);
2225 
2226     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2227     /* marshal IAudioRenderClient */
2228 
2229     hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2230     ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2231 
2232     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2233     hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2234     ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2235     if (hr == S_OK)
2236         IAudioRenderClient_Release(rcDest);
2237 
2238 
2239     IStream_Release(pStream);
2240 
2241     IAudioClient_Release(ac);
2242     IAudioRenderClient_Release(rc);
2243 
2244 }
2245 
2246 static void test_endpointvolume(void)
2247 {
2248     HRESULT hr;
2249     IAudioEndpointVolume *aev;
2250     float mindb, maxdb, increment, volume;
2251     BOOL mute;
2252 
2253     hr = IMMDevice_Activate(dev, &IID_IAudioEndpointVolume,
2254             CLSCTX_INPROC_SERVER, NULL, (void**)&aev);
2255     ok(hr == S_OK, "Activation failed with %08x\n", hr);
2256     if(hr != S_OK)
2257         return;
2258 
2259     hr = IAudioEndpointVolume_GetVolumeRange(aev, &mindb, NULL, NULL);
2260     ok(hr == E_POINTER, "GetVolumeRange should have failed with E_POINTER: 0x%08x\n", hr);
2261 
2262     hr = IAudioEndpointVolume_GetVolumeRange(aev, &mindb, &maxdb, &increment);
2263     ok(hr == S_OK, "GetVolumeRange failed: 0x%08x\n", hr);
2264     trace("got range: [%f,%f]/%f\n", mindb, maxdb, increment);
2265 
2266     hr = IAudioEndpointVolume_SetMasterVolumeLevel(aev, mindb - increment, NULL);
2267     ok(hr == E_INVALIDARG, "SetMasterVolumeLevel failed: 0x%08x\n", hr);
2268 
2269     hr = IAudioEndpointVolume_GetMasterVolumeLevel(aev, &volume);
2270     ok(hr == S_OK, "GetMasterVolumeLevel failed: 0x%08x\n", hr);
2271 
2272     hr = IAudioEndpointVolume_SetMasterVolumeLevel(aev, volume, NULL);
2273     ok(hr == S_OK, "SetMasterVolumeLevel failed: 0x%08x\n", hr);
2274 
2275     hr = IAudioEndpointVolume_GetMute(aev, &mute);
2276     ok(hr == S_OK, "GetMute failed: %08x\n", hr);
2277 
2278     hr = IAudioEndpointVolume_SetMute(aev, mute, NULL);
2279     ok(hr == S_OK || hr == S_FALSE, "SetMute failed: %08x\n", hr);
2280 
2281     IAudioEndpointVolume_Release(aev);
2282 }
2283 
2284 START_TEST(render)
2285 {
2286     HRESULT hr;
2287 
2288     CoInitializeEx(NULL, COINIT_MULTITHREADED);
2289     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2290     if (FAILED(hr))
2291     {
2292         skip("mmdevapi not available: 0x%08x\n", hr);
2293         goto cleanup;
2294     }
2295 
2296     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2297     ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2298     if (hr != S_OK || !dev)
2299     {
2300         if (hr == E_NOTFOUND)
2301             skip("No sound card available\n");
2302         else
2303             skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2304         goto cleanup;
2305     }
2306 
2307     test_audioclient();
2308     test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2309     test_formats(AUDCLNT_SHAREMODE_SHARED);
2310     test_references();
2311     test_marshal();
2312     trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2313     trace("Please redirect output to a file.\n");
2314     test_event();
2315     test_padding();
2316     test_clock(1);
2317     test_clock(0);
2318     test_session();
2319     test_streamvolume();
2320     test_channelvolume();
2321     test_simplevolume();
2322     test_volume_dependence();
2323     test_session_creation();
2324     test_worst_case();
2325     test_endpointvolume();
2326 
2327     IMMDevice_Release(dev);
2328 
2329 cleanup:
2330     if (mme)
2331         IMMDeviceEnumerator_Release(mme);
2332     CoUninitialize();
2333 }
2334