[MMDEVAPI_WINETEST] Sync with Wine Staging 1.7.37. CORE-9246
[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 slept += 100;
1230
1231 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1232 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1233 ok(pos == last, "Position %u should stop.\n", (UINT)pos);
1234
1235 /* Restart from 0 */
1236 hr = IAudioClient_Reset(ac);
1237 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1238 slept = sum = 0;
1239
1240 hr = IAudioClient_Reset(ac);
1241 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1242
1243 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1244 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1245 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1246 ok(pcpos > pcpos0, "pcpos should increase\n");
1247
1248 avail = gbsize; /* implies GetCurrentPadding == 0 */
1249 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1250 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1251 trace("data at %p\n", data);
1252
1253 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, winetest_debug>2 ?
1254 wave_generate_tone(pwfx, data, avail) : AUDCLNT_BUFFERFLAGS_SILENT);
1255 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1256 if(hr == S_OK) sum += avail;
1257
1258 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1259 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1260 ok(pad == sum, "padding %u prior to start\n", pad);
1261
1262 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1263 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1264 ok(pos == 0, "GetPosition returned non-zero pos after Reset\n");
1265 last = pos;
1266
1267 hr = IAudioClient_Start(ac); /* #3 */
1268 ok(hr == S_OK, "Start failed: %08x\n", hr);
1269
1270 Sleep(100);
1271 slept += 100;
1272
1273 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1274 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1275 trace("position %u past %ums sleep #3\n", (UINT)pos, slept);
1276 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1277 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1278 if (winetest_debug>1)
1279 ok(pos*1000/freq <= slept*1.1, "Position %u too far after playing %ums\n", (UINT)pos, slept);
1280 else
1281 skip("Rerun with WINETEST_DEBUG=2 for GetPosition tests.\n");
1282 last = pos;
1283
1284 hr = IAudioClient_Reset(ac);
1285 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset while playing: %08x\n", hr);
1286
1287 hr = IAudioClient_Stop(ac);
1288 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1289
1290 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1291 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1292
1293 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1294 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1295 trace("padding %u position %u past stop #3\n", pad, (UINT)pos);
1296 ok(pos >= last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1297 ok(pcpos > pcpos0, "pcpos should increase\n");
1298 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1299 if (pad > 0 && winetest_debug>1)
1300 ok(pos*1000/freq <= slept*1.1, "Position %u too far after stop %ums\n", (UINT)pos, slept);
1301 if(winetest_debug>1)
1302 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
1303 "Position %u after stop vs. %u padding\n", (UINT)pos, pad);
1304 last = pos;
1305
1306 /* Begin the big loop */
1307 hr = IAudioClient_Reset(ac);
1308 ok(hr == S_OK, "Reset failed: %08x\n", hr);
1309 slept = last = sum = 0;
1310 pcpos0 = pcpos;
1311
1312 ok(QueryPerformanceCounter(&hpctime0), "PerfCounter unavailable\n");
1313
1314 hr = IAudioClient_Reset(ac);
1315 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
1316
1317 hr = IAudioClient_Start(ac);
1318 ok(hr == S_OK, "Start failed: %08x\n", hr);
1319
1320 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1321 data = NULL;
1322 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1323 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
1324 trace("data at %p for prefill %u\n", data, avail);
1325
1326 if (winetest_debug>2) {
1327 hr = IAudioClient_Stop(ac);
1328 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1329
1330 Sleep(20);
1331 slept += 20;
1332
1333 hr = IAudioClient_Reset(ac);
1334 ok(hr == AUDCLNT_E_BUFFER_OPERATION_PENDING, "Reset failed: %08x\n", hr);
1335
1336 hr = IAudioClient_Start(ac);
1337 ok(hr == S_OK, "Start failed: %08x\n", hr);
1338 }
1339
1340 /* Despite passed time, data must still point to valid memory... */
1341 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1342 wave_generate_tone(pwfx, data, avail));
1343 ok(hr == S_OK, "ReleaseBuffer after stop+start failed: %08x\n", hr);
1344 if(hr == S_OK) sum += avail;
1345
1346 /* GetCurrentPadding(GCP) == 0 does not mean an underrun happened, as the
1347 * mixer may still have a little data. We believe an underrun will occur
1348 * when the mixer finds GCP smaller than a period size at the *end* of a
1349 * period cycle, i.e. shortly before calling SetEvent to signal the app
1350 * that it has ~10ms to supply data for the next cycle. IOW, a zero GCP
1351 * with no data written for over a period causes an underrun. */
1352
1353 Sleep(350);
1354 slept += 350;
1355 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1356 trace("hpctime %u after %ums\n",
1357 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart), slept);
1358
1359 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1360 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1361 ok(pos > last, "Position %u vs. last %u\n", (UINT)pos,(UINT)last);
1362 last = pos;
1363
1364 for(i=0; i < 9; i++) {
1365 Sleep(100);
1366 slept += 100;
1367
1368 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
1369 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1370
1371 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1372 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1373
1374 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1375 trace("hpctime %u pcpos %u\n",
1376 (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart),
1377 (ULONG)((pcpos-pcpos0)/10000));
1378
1379 /* Use sum-pad to see whether position is ahead padding or not. */
1380 trace("padding %u position %u/%u slept %ums iteration %d\n", pad, (UINT)pos, sum-pad, slept, i);
1381 ok(pad ? pos > last : pos >= last, "No position increase at iteration %d\n", i);
1382 ok(pos * pwfx->nSamplesPerSec <= sum * freq, "Position %u > written %u\n", (UINT)pos, sum);
1383 if (winetest_debug>1) {
1384 /* Padding does not lag behind by much */
1385 ok(pos * pwfx->nSamplesPerSec <= (sum-pad+fragment) * freq, "Position %u > written %u\n", (UINT)pos, sum);
1386 ok(pos*1000/freq <= slept*1.1, "Position %u too far after %ums\n", (UINT)pos, slept);
1387 if (pad) /* not in case of underrun */
1388 ok((pos-last)*1000/freq >= 90 && 110 >= (pos-last)*1000/freq,
1389 "Position delta %ld not regular: %ld ms\n", (long)(pos-last), (long)((pos-last)*1000/freq));
1390 }
1391 last = pos;
1392
1393 hr = IAudioClient_GetStreamLatency(ac, &t1);
1394 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1395 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1396
1397 avail = pwfx->nSamplesPerSec * 15 / 16 / 2;
1398 data = NULL;
1399 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1400 /* ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE || (hr == S_OK && i==0) without todo_wine */
1401 ok(hr == S_OK || hr == AUDCLNT_E_BUFFER_TOO_LARGE,
1402 "GetBuffer large (%u) failed: %08x\n", avail, hr);
1403 if(hr == S_OK && i) ok(FALSE, "GetBuffer large (%u) at iteration %d\n", avail, i);
1404 /* Only the first iteration should allow that large a buffer
1405 * as prefill was drained during the first 350+100ms sleep.
1406 * Afterwards, only 100ms of data should find room per iteration. */
1407
1408 if(hr == S_OK) {
1409 trace("data at %p\n", data);
1410 } else {
1411 avail = gbsize - pad;
1412 hr = IAudioRenderClient_GetBuffer(arc, avail, &data);
1413 ok(hr == S_OK, "GetBuffer small %u failed: %08x\n", avail, hr);
1414 trace("data at %p (small %u)\n", data, avail);
1415 }
1416 ok(data != NULL, "NULL buffer returned\n");
1417 if(i % 3 && !winetest_interactive) {
1418 memset(data, 0, avail * pwfx->nBlockAlign);
1419 hr = IAudioRenderClient_ReleaseBuffer(arc, avail, 0);
1420 } else {
1421 hr = IAudioRenderClient_ReleaseBuffer(arc, avail,
1422 wave_generate_tone(pwfx, data, avail));
1423 }
1424 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
1425 if(hr == S_OK) sum += avail;
1426 }
1427
1428 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1429 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1430 trace("position %u\n", (UINT)pos);
1431
1432 Sleep(1000); /* 500ms buffer underrun past full buffer */
1433
1434 hr = IAudioClient_GetCurrentPadding(ac, &pad);
1435 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
1436
1437 hr = IAudioClock_GetPosition(acl, &pos, NULL);
1438 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
1439 trace("position %u past underrun, %u padding left, %u frames written\n", (UINT)pos, pad, sum);
1440
1441 if (share) {
1442 /* Following underrun, all samples were played */
1443 ok(pad == 0, "GetCurrentPadding returned %u, should be 0\n", pad);
1444 ok(pos * pwfx->nSamplesPerSec == sum * freq,
1445 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1446 } else {
1447 /* Vista and w2k8 leave partial fragments behind */
1448 ok(pad == 0 /* w7, w2k8R2 */||
1449 pos * pwfx->nSamplesPerSec == (sum-pad) * freq, "GetCurrentPadding returned %u, should be 0\n", pad);
1450 /* expect at most 5 fragments (75ms) away */
1451 ok(pos * pwfx->nSamplesPerSec <= sum * freq &&
1452 pos * pwfx->nSamplesPerSec + 5 * fragment * freq >= sum * freq,
1453 "Position %u at end vs. %u submitted frames\n", (UINT)pos, sum);
1454 }
1455
1456 hr = IAudioClient_GetStreamLatency(ac, &t1);
1457 ok(hr == S_OK, "GetStreamLatency failed: %08x\n", hr);
1458 ok(t1 == t2, "Latency not constant, delta %ld\n", (long)(t1-t2));
1459
1460 ok(QueryPerformanceCounter(&hpctime), "PerfCounter failed\n");
1461 trace("hpctime %u after underrun\n", (ULONG)((hpctime.QuadPart-hpctime0.QuadPart)*1000/hpcfreq.QuadPart));
1462
1463 hr = IAudioClient_Stop(ac);
1464 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1465
1466 CoTaskMemFree(pwfx);
1467
1468 IAudioClock_Release(acl);
1469 IAudioRenderClient_Release(arc);
1470 IAudioClient_Release(ac);
1471 }
1472
1473 static void test_session(void)
1474 {
1475 IAudioClient *ses1_ac1, *ses1_ac2, *cap_ac;
1476 IAudioSessionControl2 *ses1_ctl, *ses1_ctl2, *cap_ctl = NULL;
1477 IMMDevice *cap_dev;
1478 GUID ses1_guid;
1479 AudioSessionState state;
1480 WAVEFORMATEX *pwfx;
1481 ULONG ref;
1482 HRESULT hr;
1483
1484 hr = CoCreateGuid(&ses1_guid);
1485 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1486
1487 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1488 NULL, (void**)&ses1_ac1);
1489 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1490 if (FAILED(hr)) return;
1491
1492 hr = IAudioClient_GetMixFormat(ses1_ac1, &pwfx);
1493 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1494
1495 hr = IAudioClient_Initialize(ses1_ac1, AUDCLNT_SHAREMODE_SHARED,
1496 0, 5000000, 0, pwfx, &ses1_guid);
1497 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1498
1499 if(hr == S_OK){
1500 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1501 NULL, (void**)&ses1_ac2);
1502 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1503 }
1504 if(hr != S_OK){
1505 skip("Unable to open the same device twice. Skipping session tests\n");
1506
1507 ref = IAudioClient_Release(ses1_ac1);
1508 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1509 CoTaskMemFree(pwfx);
1510 return;
1511 }
1512
1513 hr = IAudioClient_Initialize(ses1_ac2, AUDCLNT_SHAREMODE_SHARED,
1514 0, 5000000, 0, pwfx, &ses1_guid);
1515 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1516
1517 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
1518 eMultimedia, &cap_dev);
1519 if(hr == S_OK){
1520 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1521 NULL, (void**)&cap_ac);
1522 ok((hr == S_OK)^(cap_ac == NULL), "Activate %08x &out pointer\n", hr);
1523 ok(hr == S_OK, "Activate failed: %08x\n", hr);
1524 IMMDevice_Release(cap_dev);
1525 }
1526 if(hr == S_OK){
1527 WAVEFORMATEX *cap_pwfx;
1528
1529 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
1530 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1531
1532 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
1533 0, 5000000, 0, cap_pwfx, &ses1_guid);
1534 ok(hr == S_OK, "Initialize failed for capture in rendering session: %08x\n", hr);
1535 CoTaskMemFree(cap_pwfx);
1536 }
1537 if(hr == S_OK){
1538 hr = IAudioClient_GetService(cap_ac, &IID_IAudioSessionControl, (void**)&cap_ctl);
1539 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1540 if(FAILED(hr))
1541 cap_ctl = NULL;
1542 }else
1543 skip("No capture session: %08x; skipping capture device in render session tests\n", hr);
1544
1545 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl2, (void**)&ses1_ctl);
1546 ok(hr == E_NOINTERFACE, "GetService gave wrong error: %08x\n", hr);
1547
1548 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl);
1549 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1550
1551 hr = IAudioClient_GetService(ses1_ac1, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1552 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1553 ok(ses1_ctl == ses1_ctl2, "Got different controls: %p %p\n", ses1_ctl, ses1_ctl2);
1554 ref = IAudioSessionControl2_Release(ses1_ctl2);
1555 ok(ref != 0, "AudioSessionControl was destroyed\n");
1556
1557 hr = IAudioClient_GetService(ses1_ac2, &IID_IAudioSessionControl, (void**)&ses1_ctl2);
1558 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1559
1560 hr = IAudioSessionControl2_GetState(ses1_ctl, NULL);
1561 ok(hr == NULL_PTR_ERR, "GetState gave wrong error: %08x\n", hr);
1562
1563 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1564 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1565 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1566
1567 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1568 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1569 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1570
1571 if(cap_ctl){
1572 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1573 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1574 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1575 }
1576
1577 hr = IAudioClient_Start(ses1_ac1);
1578 ok(hr == S_OK, "Start failed: %08x\n", hr);
1579
1580 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1581 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1582 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1583
1584 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1585 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1586 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1587
1588 if(cap_ctl){
1589 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1590 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1591 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1592 }
1593
1594 hr = IAudioClient_Stop(ses1_ac1);
1595 ok(hr == S_OK, "Start failed: %08x\n", hr);
1596
1597 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1598 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1599 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1600
1601 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1602 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1603 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1604
1605 if(cap_ctl){
1606 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1607 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1608 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1609
1610 hr = IAudioClient_Start(cap_ac);
1611 ok(hr == S_OK, "Start failed: %08x\n", hr);
1612
1613 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1614 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1615 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1616
1617 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1618 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1619 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1620
1621 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1622 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1623 ok(state == AudioSessionStateActive, "Got wrong state: %d\n", state);
1624
1625 hr = IAudioClient_Stop(cap_ac);
1626 ok(hr == S_OK, "Stop failed: %08x\n", hr);
1627
1628 hr = IAudioSessionControl2_GetState(ses1_ctl, &state);
1629 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1630 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1631
1632 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1633 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1634 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1635
1636 hr = IAudioSessionControl2_GetState(cap_ctl, &state);
1637 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1638 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1639
1640 ref = IAudioSessionControl2_Release(cap_ctl);
1641 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1642
1643 ref = IAudioClient_Release(cap_ac);
1644 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1645 }
1646
1647 ref = IAudioSessionControl2_Release(ses1_ctl);
1648 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1649
1650 ref = IAudioClient_Release(ses1_ac1);
1651 ok(ref == 0, "AudioClient wasn't released: %u\n", ref);
1652
1653 ref = IAudioClient_Release(ses1_ac2);
1654 ok(ref == 1, "AudioClient had wrong refcount: %u\n", ref);
1655
1656 /* we've released all of our IAudioClient references, so check GetState */
1657 hr = IAudioSessionControl2_GetState(ses1_ctl2, &state);
1658 ok(hr == S_OK, "GetState failed: %08x\n", hr);
1659 ok(state == AudioSessionStateInactive, "Got wrong state: %d\n", state);
1660
1661 ref = IAudioSessionControl2_Release(ses1_ctl2);
1662 ok(ref == 0, "AudioSessionControl wasn't released: %u\n", ref);
1663
1664 CoTaskMemFree(pwfx);
1665 }
1666
1667 static void test_streamvolume(void)
1668 {
1669 IAudioClient *ac;
1670 IAudioStreamVolume *asv;
1671 WAVEFORMATEX *fmt;
1672 UINT32 chans, i;
1673 HRESULT hr;
1674 float vol, *vols;
1675
1676 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1677 NULL, (void**)&ac);
1678 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1679 if(hr != S_OK)
1680 return;
1681
1682 hr = IAudioClient_GetMixFormat(ac, &fmt);
1683 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1684
1685 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1686 0, fmt, NULL);
1687 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1688
1689 if(hr == S_OK){
1690 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
1691 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1692 }
1693 if(hr != S_OK){
1694 IAudioClient_Release(ac);
1695 CoTaskMemFree(fmt);
1696 return;
1697 }
1698
1699 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
1700 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1701
1702 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
1703 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1704 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1705
1706 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
1707 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1708
1709 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
1710 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1711
1712 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
1713 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
1714
1715 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1716 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1717 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1718
1719 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
1720 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1721
1722 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
1723 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1724
1725 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
1726 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1727
1728 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
1729 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1730
1731 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
1732 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1733 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1734
1735 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
1736 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1737
1738 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
1739 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
1740
1741 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1742 ok(vols != NULL, "HeapAlloc failed\n");
1743
1744 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
1745 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1746
1747 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
1748 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1749 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1750 for(i = 1; i < fmt->nChannels; ++i)
1751 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1752
1753 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
1754 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1755
1756 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
1757 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
1758
1759 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
1760 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1761
1762 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
1763 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1764
1765 HeapFree(GetProcessHeap(), 0, vols);
1766 IAudioStreamVolume_Release(asv);
1767 IAudioClient_Release(ac);
1768 CoTaskMemFree(fmt);
1769 }
1770
1771 static void test_channelvolume(void)
1772 {
1773 IAudioClient *ac;
1774 IChannelAudioVolume *acv;
1775 WAVEFORMATEX *fmt;
1776 UINT32 chans, i;
1777 HRESULT hr;
1778 float vol, *vols;
1779
1780 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1781 NULL, (void**)&ac);
1782 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1783 if(hr != S_OK)
1784 return;
1785
1786 hr = IAudioClient_GetMixFormat(ac, &fmt);
1787 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1788
1789 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1790 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1791 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1792
1793 if(hr == S_OK){
1794 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
1795 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1796 }
1797 if(hr != S_OK){
1798 IAudioClient_Release(ac);
1799 CoTaskMemFree(fmt);
1800 return;
1801 }
1802
1803 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
1804 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1805
1806 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
1807 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1808 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
1809
1810 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
1811 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1812
1813 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
1814 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
1815
1816 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
1817 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
1818
1819 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1820 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1821 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
1822
1823 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
1824 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1825
1826 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
1827 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1828
1829 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
1830 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
1831
1832 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
1833 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1834
1835 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
1836 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
1837 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
1838
1839 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
1840 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1841
1842 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
1843 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
1844
1845 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1846 ok(vols != NULL, "HeapAlloc failed\n");
1847
1848 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
1849 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
1850
1851 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
1852 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
1853 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
1854 for(i = 1; i < fmt->nChannels; ++i)
1855 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
1856
1857 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
1858 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1859
1860 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
1861 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
1862
1863 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
1864 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
1865
1866 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
1867 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
1868
1869 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
1870 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
1871
1872 HeapFree(GetProcessHeap(), 0, vols);
1873 IChannelAudioVolume_Release(acv);
1874 IAudioClient_Release(ac);
1875 CoTaskMemFree(fmt);
1876 }
1877
1878 static void test_simplevolume(void)
1879 {
1880 IAudioClient *ac;
1881 ISimpleAudioVolume *sav;
1882 WAVEFORMATEX *fmt;
1883 HRESULT hr;
1884 float vol;
1885 BOOL mute;
1886
1887 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1888 NULL, (void**)&ac);
1889 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1890 if(hr != S_OK)
1891 return;
1892
1893 hr = IAudioClient_GetMixFormat(ac, &fmt);
1894 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1895
1896 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1897 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
1898 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1899
1900 if(hr == S_OK){
1901 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1902 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1903 }
1904 if(hr != S_OK){
1905 IAudioClient_Release(ac);
1906 CoTaskMemFree(fmt);
1907 return;
1908 }
1909
1910 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
1911 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
1912
1913 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1914 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1915 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
1916
1917 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
1918 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1919
1920 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
1921 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
1922
1923 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
1924 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1925
1926 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1927 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1928 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1929
1930 hr = ISimpleAudioVolume_GetMute(sav, NULL);
1931 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
1932
1933 mute = TRUE;
1934 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1935 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1936 ok(mute == FALSE, "Session is already muted\n");
1937
1938 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
1939 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1940
1941 mute = FALSE;
1942 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1943 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1944 ok(mute == TRUE, "Session should have been muted\n");
1945
1946 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
1947 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
1948 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
1949
1950 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1951 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
1952
1953 mute = FALSE;
1954 hr = ISimpleAudioVolume_GetMute(sav, &mute);
1955 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
1956 ok(mute == TRUE, "Session should have been muted\n");
1957
1958 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
1959 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
1960
1961 ISimpleAudioVolume_Release(sav);
1962 IAudioClient_Release(ac);
1963 CoTaskMemFree(fmt);
1964 }
1965
1966 static void test_volume_dependence(void)
1967 {
1968 IAudioClient *ac, *ac2;
1969 ISimpleAudioVolume *sav;
1970 IChannelAudioVolume *cav;
1971 IAudioStreamVolume *asv;
1972 WAVEFORMATEX *fmt;
1973 HRESULT hr;
1974 float vol;
1975 GUID session;
1976 UINT32 nch;
1977
1978 hr = CoCreateGuid(&session);
1979 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
1980
1981 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1982 NULL, (void**)&ac);
1983 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1984 if(hr != S_OK)
1985 return;
1986
1987 hr = IAudioClient_GetMixFormat(ac, &fmt);
1988 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1989
1990 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
1991 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
1992 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1993
1994 if(hr == S_OK){
1995 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
1996 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
1997 }
1998 if(hr != S_OK){
1999 IAudioClient_Release(ac);
2000 CoTaskMemFree(fmt);
2001 return;
2002 }
2003
2004 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
2005 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
2006
2007 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
2008 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
2009
2010 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
2011 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
2012
2013 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
2014 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
2015
2016 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
2017 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
2018
2019 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
2020 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
2021 ok(fabsf(vol - 0.2) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
2022
2023 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
2024 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
2025 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
2026
2027 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2028 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
2029 ok(fabsf(vol - 0.6) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
2030
2031 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2032 NULL, (void**)&ac2);
2033 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2034
2035 if(hr == S_OK){
2036 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
2037 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
2038 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2039 if(hr != S_OK)
2040 IAudioClient_Release(ac2);
2041 }
2042
2043 if(hr == S_OK){
2044 IChannelAudioVolume *cav2;
2045 IAudioStreamVolume *asv2;
2046
2047 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
2048 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2049
2050 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
2051 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2052
2053 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
2054 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
2055 ok(fabsf(vol - 0.4) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
2056
2057 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
2058 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
2059 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
2060
2061 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
2062 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
2063 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
2064
2065 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
2066 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
2067 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
2068
2069 IAudioStreamVolume_Release(asv2);
2070 IChannelAudioVolume_Release(cav2);
2071 IAudioClient_Release(ac2);
2072 }else
2073 skip("Unable to open the same device twice. Skipping session volume control tests\n");
2074
2075 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
2076 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
2077
2078 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
2079 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
2080
2081 CoTaskMemFree(fmt);
2082 ISimpleAudioVolume_Release(sav);
2083 IChannelAudioVolume_Release(cav);
2084 IAudioStreamVolume_Release(asv);
2085 IAudioClient_Release(ac);
2086 }
2087
2088 static void test_session_creation(void)
2089 {
2090 IMMDevice *cap_dev;
2091 IAudioClient *ac;
2092 IAudioSessionManager *sesm;
2093 ISimpleAudioVolume *sav;
2094 GUID session_guid;
2095 float vol;
2096 HRESULT hr;
2097 WAVEFORMATEX *fmt;
2098
2099 CoCreateGuid(&session_guid);
2100
2101 hr = IMMDevice_Activate(dev, &IID_IAudioSessionManager,
2102 CLSCTX_INPROC_SERVER, NULL, (void**)&sesm);
2103 ok((hr == S_OK)^(sesm == NULL), "Activate %08x &out pointer\n", hr);
2104 ok(hr == S_OK, "Activate failed: %08x\n", hr);
2105
2106 hr = IAudioSessionManager_GetSimpleAudioVolume(sesm, &session_guid,
2107 FALSE, &sav);
2108 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
2109
2110 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
2111 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
2112
2113 /* Release completely to show session persistence */
2114 ISimpleAudioVolume_Release(sav);
2115 IAudioSessionManager_Release(sesm);
2116
2117 /* test if we can create a capture audioclient in the session we just
2118 * created from a SessionManager derived from a render device */
2119 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture,
2120 eMultimedia, &cap_dev);
2121 if(hr == S_OK){
2122 WAVEFORMATEX *cap_pwfx;
2123 IAudioClient *cap_ac;
2124 ISimpleAudioVolume *cap_sav;
2125 IAudioSessionManager *cap_sesm;
2126
2127 hr = IMMDevice_Activate(cap_dev, &IID_IAudioSessionManager,
2128 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_sesm);
2129 ok((hr == S_OK)^(cap_sesm == NULL), "Activate %08x &out pointer\n", hr);
2130 ok(hr == S_OK, "Activate failed: %08x\n", hr);
2131
2132 hr = IAudioSessionManager_GetSimpleAudioVolume(cap_sesm, &session_guid,
2133 FALSE, &cap_sav);
2134 ok(hr == S_OK, "GetSimpleAudioVolume failed: %08x\n", hr);
2135
2136 vol = 0.5f;
2137 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2138 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2139 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
2140
2141 ISimpleAudioVolume_Release(cap_sav);
2142 IAudioSessionManager_Release(cap_sesm);
2143
2144 hr = IMMDevice_Activate(cap_dev, &IID_IAudioClient,
2145 CLSCTX_INPROC_SERVER, NULL, (void**)&cap_ac);
2146 ok(hr == S_OK, "Activate failed: %08x\n", hr);
2147
2148 IMMDevice_Release(cap_dev);
2149
2150 hr = IAudioClient_GetMixFormat(cap_ac, &cap_pwfx);
2151 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2152
2153 hr = IAudioClient_Initialize(cap_ac, AUDCLNT_SHAREMODE_SHARED,
2154 0, 5000000, 0, cap_pwfx, &session_guid);
2155 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2156
2157 CoTaskMemFree(cap_pwfx);
2158
2159 if(hr == S_OK){
2160 hr = IAudioClient_GetService(cap_ac, &IID_ISimpleAudioVolume,
2161 (void**)&cap_sav);
2162 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2163 }
2164 if(hr == S_OK){
2165 vol = 0.5f;
2166 hr = ISimpleAudioVolume_GetMasterVolume(cap_sav, &vol);
2167 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2168 ok(vol == 1.f, "Got wrong volume: %f\n", vol);
2169
2170 ISimpleAudioVolume_Release(cap_sav);
2171 }
2172
2173 IAudioClient_Release(cap_ac);
2174 }
2175
2176 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2177 NULL, (void**)&ac);
2178 ok((hr == S_OK)^(ac == NULL), "Activate %08x &out pointer\n", hr);
2179 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2180 if(hr != S_OK)
2181 return;
2182
2183 hr = IAudioClient_GetMixFormat(ac, &fmt);
2184 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2185
2186 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2187 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session_guid);
2188 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2189
2190 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
2191 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2192 if(hr == S_OK){
2193 vol = 0.5f;
2194 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
2195 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
2196 ok(fabs(vol - 0.6f) < 0.05f, "Got wrong volume: %f\n", vol);
2197
2198 ISimpleAudioVolume_Release(sav);
2199 }
2200
2201 CoTaskMemFree(fmt);
2202 IAudioClient_Release(ac);
2203 }
2204
2205 static void test_worst_case(void)
2206 {
2207 HANDLE event;
2208 HRESULT hr;
2209 IAudioClient *ac;
2210 IAudioRenderClient *arc;
2211 IAudioClock *acl;
2212 WAVEFORMATEX *pwfx;
2213 REFERENCE_TIME defp;
2214 UINT64 freq, pos, pcpos0, pcpos;
2215 BYTE *data;
2216 DWORD r;
2217 UINT32 pad, fragment, sum;
2218 int i,j;
2219
2220 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2221 NULL, (void**)&ac);
2222 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2223 if(hr != S_OK)
2224 return;
2225
2226 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2227 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2228
2229 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
2230 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 500000, 0, pwfx, NULL);
2231 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2232 if(hr != S_OK)
2233 return;
2234
2235 hr = IAudioClient_GetDevicePeriod(ac, &defp, NULL);
2236 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
2237
2238 fragment = MulDiv(defp, pwfx->nSamplesPerSec, 10000000);
2239
2240 event = CreateEventW(NULL, FALSE, FALSE, NULL);
2241 ok(event != NULL, "CreateEvent failed\n");
2242
2243 hr = IAudioClient_SetEventHandle(ac, event);
2244 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
2245
2246 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
2247 ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr);
2248
2249 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
2250 ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr);
2251
2252 hr = IAudioClock_GetFrequency(acl, &freq);
2253 ok(hr == S_OK, "GetFrequency failed: %08x\n", hr);
2254
2255 for(j = 0; j <= (winetest_interactive ? 9 : 2); j++){
2256 sum = 0;
2257 trace("Should play %ums continuous tone with fragment size %u.\n",
2258 (ULONG)(defp/100), fragment);
2259
2260 hr = IAudioClock_GetPosition(acl, &pos, &pcpos0);
2261 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2262
2263 /* XAudio2 prefills one period, play without it */
2264 if(winetest_debug>2){
2265 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2266 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2267
2268 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment, AUDCLNT_BUFFERFLAGS_SILENT);
2269 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2270 if(hr == S_OK)
2271 sum += fragment;
2272 }
2273
2274 hr = IAudioClient_Start(ac);
2275 ok(hr == S_OK, "Start failed: %08x\n", hr);
2276
2277 for(i = 0; i <= 99; i++){ /* 100 x 10ms = 1 second */
2278 r = WaitForSingleObject(event, 60 + defp / 10000);
2279 ok(r == WAIT_OBJECT_0, "Wait iteration %d gave %x\n", i, r);
2280
2281 /* the app has nearly one period time to feed data */
2282 Sleep((i % 10) * defp / 120000);
2283
2284 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2285 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2286
2287 /* XAudio2 writes only when there's little data left */
2288 if(pad <= fragment){
2289 hr = IAudioRenderClient_GetBuffer(arc, fragment, &data);
2290 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
2291
2292 hr = IAudioRenderClient_ReleaseBuffer(arc, fragment,
2293 wave_generate_tone(pwfx, data, fragment));
2294 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
2295 if(hr == S_OK)
2296 sum += fragment;
2297 }
2298 }
2299
2300 hr = IAudioClient_Stop(ac);
2301 ok(hr == S_OK, "Stop failed: %08x\n", hr);
2302
2303 hr = IAudioClient_GetCurrentPadding(ac, &pad);
2304 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
2305
2306 hr = IAudioClock_GetPosition(acl, &pos, &pcpos);
2307 ok(hr == S_OK, "GetPosition failed: %08x\n", hr);
2308
2309 Sleep(100);
2310
2311 trace("Released %u=%ux%u -%u frames at %u worth %ums in %ums\n",
2312 sum, sum/fragment, fragment, pad,
2313 pwfx->nSamplesPerSec, MulDiv(sum-pad, 1000, pwfx->nSamplesPerSec),
2314 (ULONG)((pcpos-pcpos0)/10000));
2315
2316 ok(pos * pwfx->nSamplesPerSec == (sum-pad) * freq,
2317 "Position %u at end vs. %u-%u submitted frames\n", (UINT)pos, sum, pad);
2318
2319 hr = IAudioClient_Reset(ac);
2320 ok(hr == S_OK, "Reset failed: %08x\n", hr);
2321
2322 Sleep(250);
2323 }
2324
2325 CoTaskMemFree(pwfx);
2326 IAudioClient_Release(ac);
2327 IAudioClock_Release(acl);
2328 IAudioRenderClient_Release(arc);
2329 }
2330
2331 static void test_marshal(void)
2332 {
2333 IStream *pStream;
2334 IAudioClient *ac, *acDest;
2335 IAudioRenderClient *rc, *rcDest;
2336 WAVEFORMATEX *pwfx;
2337 HRESULT hr;
2338
2339 /* IAudioRenderClient */
2340 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
2341 NULL, (void**)&ac);
2342 ok(hr == S_OK, "Activation failed with %08x\n", hr);
2343 if(hr != S_OK)
2344 return;
2345
2346 hr = IAudioClient_GetMixFormat(ac, &pwfx);
2347 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
2348
2349 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
2350 0, pwfx, NULL);
2351 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
2352
2353 CoTaskMemFree(pwfx);
2354
2355 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
2356 ok(hr == S_OK, "GetService failed: %08x\n", hr);
2357 if(hr != S_OK) {
2358 IAudioClient_Release(ac);
2359 return;
2360 }
2361
2362 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
2363 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
2364
2365 /* marshal IAudioClient */
2366
2367 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2368 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
2369
2370 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2371 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
2372 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
2373 if (hr == S_OK)
2374 IAudioClient_Release(acDest);
2375
2376 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2377 /* marshal IAudioRenderClient */
2378
2379 hr = CoMarshalInterface(pStream, &IID_IAudioRenderClient, (IUnknown*)rc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
2380 ok(hr == S_OK, "CoMarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2381
2382 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
2383 hr = CoUnmarshalInterface(pStream, &IID_IAudioRenderClient, (void **)&rcDest);
2384 ok(hr == S_OK, "CoUnmarshalInterface IAudioRenderClient failed 0x%08x\n", hr);
2385 if (hr == S_OK)
2386 IAudioRenderClient_Release(rcDest);
2387
2388
2389 IStream_Release(pStream);
2390
2391 IAudioClient_Release(ac);
2392 IAudioRenderClient_Release(rc);
2393
2394 }
2395
2396 START_TEST(render)
2397 {
2398 HRESULT hr;
2399
2400 CoInitializeEx(NULL, COINIT_MULTITHREADED);
2401 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
2402 if (FAILED(hr))
2403 {
2404 skip("mmdevapi not available: 0x%08x\n", hr);
2405 goto cleanup;
2406 }
2407
2408 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
2409 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
2410 if (hr != S_OK || !dev)
2411 {
2412 if (hr == E_NOTFOUND)
2413 skip("No sound card available\n");
2414 else
2415 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
2416 goto cleanup;
2417 }
2418
2419 test_audioclient();
2420 test_formats(AUDCLNT_SHAREMODE_EXCLUSIVE);
2421 test_formats(AUDCLNT_SHAREMODE_SHARED);
2422 test_formats2();
2423 test_references();
2424 test_marshal();
2425 trace("Output to a MS-DOS console is particularly slow and disturbs timing.\n");
2426 trace("Please redirect output to a file.\n");
2427 test_event();
2428 test_padding();
2429 test_clock(1);
2430 test_clock(0);
2431 test_session();
2432 test_streamvolume();
2433 test_channelvolume();
2434 test_simplevolume();
2435 test_volume_dependence();
2436 test_session_creation();
2437 test_worst_case();
2438
2439 IMMDevice_Release(dev);
2440
2441 cleanup:
2442 if (mme)
2443 IMMDeviceEnumerator_Release(mme);
2444 CoUninitialize();
2445 }