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