[MMDEVAPI_WINETEST] Sync with Wine Staging 1.7.47. CORE-9924
[reactos.git] / rostests / winetests / mmdevapi / capture.c
1 /*
2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 /* This test is for audio capture specific mechanisms
20 * Tests:
21 * - IAudioClient with eCapture and IAudioCaptureClient
22 */
23
24 #include <math.h>
25
26 #include "wine/test.h"
27
28 #define COBJMACROS
29
30 #ifdef STANDALONE
31 #include "initguid.h"
32 #endif
33
34 #include "unknwn.h"
35 #include "uuids.h"
36 #include "mmdeviceapi.h"
37 #include "audioclient.h"
38
39 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
40
41 static IMMDevice *dev = NULL;
42 static const LARGE_INTEGER ullZero;
43
44 static void test_uninitialized(IAudioClient *ac)
45 {
46 HRESULT hr;
47 UINT32 num;
48 REFERENCE_TIME t1;
49
50 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
51 IUnknown *unk;
52
53 hr = IAudioClient_GetBufferSize(ac, &num);
54 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
55
56 hr = IAudioClient_GetStreamLatency(ac, &t1);
57 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
58
59 hr = IAudioClient_GetCurrentPadding(ac, &num);
60 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
61
62 hr = IAudioClient_Start(ac);
63 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
64
65 hr = IAudioClient_Stop(ac);
66 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
67
68 hr = IAudioClient_Reset(ac);
69 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
70
71 hr = IAudioClient_SetEventHandle(ac, handle);
72 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
73
74 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
75 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
76
77 CloseHandle(handle);
78 }
79
80 static void test_capture(IAudioClient *ac, HANDLE handle, WAVEFORMATEX *wfx)
81 {
82 IAudioCaptureClient *acc;
83 HRESULT hr;
84 UINT32 frames, next, pad, sum = 0;
85 BYTE *data;
86 DWORD flags, r;
87 UINT64 pos, qpc;
88 REFERENCE_TIME period;
89
90 hr = IAudioClient_GetService(ac, &IID_IAudioCaptureClient, (void**)&acc);
91 ok(hr == S_OK, "IAudioClient_GetService(IID_IAudioCaptureClient) returns %08x\n", hr);
92 if (hr != S_OK)
93 return;
94
95 frames = 0xabadcafe;
96 data = (void*)0xdeadf00d;
97 flags = 0xabadcafe;
98 pos = qpc = 0xdeadbeef;
99 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
100 ok(hr == AUDCLNT_S_BUFFER_EMPTY, "Initial IAudioCaptureClient_GetBuffer returns %08x\n", hr);
101
102 /* should be empty right after start. Otherwise consume one packet */
103 if(hr == S_OK){
104 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
105 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
106 sum += frames;
107
108 frames = 0xabadcafe;
109 data = (void*)0xdeadf00d;
110 flags = 0xabadcafe;
111 pos = qpc = 0xdeadbeef;
112 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
113 ok(hr == AUDCLNT_S_BUFFER_EMPTY, "Initial IAudioCaptureClient_GetBuffer returns %08x\n", hr);
114 }
115
116 if(hr == AUDCLNT_S_BUFFER_EMPTY){
117 ok(!frames, "frames changed to %u\n", frames);
118 ok(data == (void*)0xdeadf00d, "data changed to %p\n", data);
119 ok(flags == 0xabadcafe, "flags changed to %x\n", flags);
120 ok(pos == 0xdeadbeef, "position changed to %u\n", (UINT)pos);
121 ok(qpc == 0xdeadbeef, "timer changed to %u\n", (UINT)qpc);
122
123 /* GetNextPacketSize yields 0 if no data is yet available
124 * it is not constantly period_size * SamplesPerSec */
125 hr = IAudioCaptureClient_GetNextPacketSize(acc, &next);
126 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
127 ok(!next, "GetNextPacketSize %u\n", next);
128 }
129
130 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
131 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
132 sum += frames;
133
134 ok(ResetEvent(handle), "ResetEvent\n");
135
136 hr = IAudioCaptureClient_GetNextPacketSize(acc, &next);
137 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
138
139 hr = IAudioClient_GetCurrentPadding(ac, &pad);
140 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
141 ok(next == pad, "GetNextPacketSize %u vs. GCP %u\n", next, pad);
142 /* later GCP will grow, while GNPS is 0 or period size */
143
144 hr = IAudioCaptureClient_GetNextPacketSize(acc, NULL);
145 ok(hr == E_POINTER, "IAudioCaptureClient_GetNextPacketSize(NULL) returns %08x\n", hr);
146
147 data = (void*)0xdeadf00d;
148 frames = 0xdeadbeef;
149 flags = 0xabadcafe;
150 hr = IAudioCaptureClient_GetBuffer(acc, &data, NULL, NULL, NULL, NULL);
151 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(data, NULL, NULL) returns %08x\n", hr);
152
153 hr = IAudioCaptureClient_GetBuffer(acc, NULL, &frames, NULL, NULL, NULL);
154 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(NULL, &frames, NULL) returns %08x\n", hr);
155
156 hr = IAudioCaptureClient_GetBuffer(acc, NULL, NULL, &flags, NULL, NULL);
157 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(NULL, NULL, &flags) returns %08x\n", hr);
158
159 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, NULL, NULL, NULL);
160 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(&ata, &frames, NULL) returns %08x\n", hr);
161 ok((DWORD_PTR)data == 0xdeadf00d, "data is reset to %p\n", data);
162 ok(frames == 0xdeadbeef, "frames is reset to %08x\n", frames);
163 ok(flags == 0xabadcafe, "flags is reset to %08x\n", flags);
164
165 hr = IAudioClient_GetDevicePeriod(ac, &period, NULL);
166 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
167 period = MulDiv(period, wfx->nSamplesPerSec, 10000000); /* as in render.c */
168
169 ok(WaitForSingleObject(handle, 1000) == WAIT_OBJECT_0, "Waiting on event handle failed!\n");
170
171 data = (void*)0xdeadf00d;
172 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
173 ok(hr == S_OK || hr == AUDCLNT_S_BUFFER_EMPTY, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
174 if (hr == S_OK){
175 ok(frames, "Amount of frames locked is 0!\n");
176 /* broken: some w7 machines return pad == 0 and DATA_DISCONTINUITY here,
177 * AUDCLNT_S_BUFFER_EMPTY above, yet pos == 1-2 * period rather than 0 */
178 ok(pos == sum || broken(pos == period || pos == 2*period),
179 "Position %u expected %u\n", (UINT)pos, sum);
180 sum = pos;
181 }else if (hr == AUDCLNT_S_BUFFER_EMPTY){
182 ok(!frames, "Amount of frames locked with empty buffer is %u!\n", frames);
183 ok(data == (void*)0xdeadf00d, "No data changed to %p\n", data);
184 }
185
186 trace("Wait'ed position %d pad %u flags %x, amount of frames locked: %u\n",
187 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
188
189 hr = IAudioCaptureClient_GetNextPacketSize(acc, &next);
190 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
191 ok(next == frames, "GetNextPacketSize %u vs. GetBuffer %u\n", next, frames);
192
193 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
194 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
195
196 hr = IAudioCaptureClient_ReleaseBuffer(acc, 0);
197 ok(hr == S_OK, "Releasing 0 returns %08x\n", hr);
198
199 hr = IAudioCaptureClient_GetNextPacketSize(acc, &next);
200 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
201
202 if (frames) {
203 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
204 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "Releasing buffer twice returns %08x\n", hr);
205 sum += frames;
206 }
207
208 Sleep(350); /* for sure there's data now */
209
210 hr = IAudioClient_GetCurrentPadding(ac, &pad);
211 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
212
213 /** GetNextPacketSize
214 * returns either 0 or one period worth of frames
215 * whereas GetCurrentPadding grows when input is not consumed. */
216 hr = IAudioCaptureClient_GetNextPacketSize(acc, &next);
217 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
218 ok(next < pad, "GetNextPacketSize %u vs. GCP %u\n", next, pad);
219
220 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
221 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
222 ok(next == frames, "GetNextPacketSize %u vs. GetBuffer %u\n", next, frames);
223
224 if(hr == S_OK){
225 UINT32 frames2 = frames;
226 UINT64 pos2, qpc2;
227 ok(frames, "Amount of frames locked is 0!\n");
228 ok(pos == sum, "Position %u expected %u\n", (UINT)pos, sum);
229
230 hr = IAudioCaptureClient_ReleaseBuffer(acc, 0);
231 ok(hr == S_OK, "Releasing 0 returns %08x\n", hr);
232
233 /* GCP did not decrement, no data consumed */
234 hr = IAudioClient_GetCurrentPadding(ac, &frames);
235 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
236 ok(frames == pad || frames == pad + next /* concurrent feeder */,
237 "GCP %u past ReleaseBuffer(0) initially %u\n", frames, pad);
238
239 /* should re-get the same data */
240 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos2, &qpc2);
241 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
242 ok(frames2 == frames, "GetBuffer after ReleaseBuffer(0) %u/%u\n", frames2, frames);
243 ok(pos2 == pos, "Position after ReleaseBuffer(0) %u/%u\n", (UINT)pos2, (UINT)pos);
244 todo_wine ok(qpc2 == qpc, "HPC after ReleaseBuffer(0) %u vs. %u\n", (UINT)qpc2, (UINT)qpc);
245 }
246
247 /* trace after the GCP test because log output to MS-DOS console disturbs timing */
248 trace("Sleep.1 position %d pad %u flags %x, amount of frames locked: %u\n",
249 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
250
251 if(hr == S_OK){
252 UINT32 frames2 = 0xabadcafe;
253 BYTE *data2 = (void*)0xdeadf00d;
254 flags = 0xabadcafe;
255
256 ok(pos == sum, "Position %u expected %u\n", (UINT)pos, sum);
257
258 pos = qpc = 0xdeadbeef;
259 hr = IAudioCaptureClient_GetBuffer(acc, &data2, &frames2, &flags, &pos, &qpc);
260 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "Out of order IAudioCaptureClient_GetBuffer returns %08x\n", hr);
261 ok(frames2 == 0xabadcafe, "Out of order frames changed to %x\n", frames2);
262 ok(data2 == (void*)0xdeadf00d, "Out of order data changed to %p\n", data2);
263 ok(flags == 0xabadcafe, "Out of order flags changed to %x\n", flags);
264 ok(pos == 0xdeadbeef, "Out of order position changed to %x\n", (UINT)pos);
265 ok(qpc == 0xdeadbeef, "Out of order timer changed to %x\n", (UINT)qpc);
266
267 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames+1);
268 ok(hr == AUDCLNT_E_INVALID_SIZE, "Releasing buffer+1 returns %08x\n", hr);
269
270 hr = IAudioCaptureClient_ReleaseBuffer(acc, 1);
271 ok(hr == AUDCLNT_E_INVALID_SIZE, "Releasing 1 returns %08x\n", hr);
272
273 hr = IAudioClient_Reset(ac);
274 ok(hr == AUDCLNT_E_NOT_STOPPED, "Reset failed: %08x\n", hr);
275 }
276
277 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
278 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
279
280 if (frames) {
281 sum += frames;
282 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
283 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "Releasing buffer twice returns %08x\n", hr);
284 }
285
286 frames = period;
287 ok(next == frames, "GetNextPacketSize %u vs. GetDevicePeriod %u\n", next, frames);
288
289 /* GetBufferSize is not a multiple of the period size! */
290 hr = IAudioClient_GetBufferSize(ac, &next);
291 ok(hr == S_OK, "GetBufferSize failed: %08x\n", hr);
292 trace("GetBufferSize %u period size %u\n", next, frames);
293
294 Sleep(400); /* overrun */
295
296 hr = IAudioClient_GetCurrentPadding(ac, &pad);
297 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
298
299 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
300 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
301
302 trace("Overrun position %d pad %u flags %x, amount of frames locked: %u\n",
303 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
304
305 if(hr == S_OK){
306 /* The discontinuity is reported here, but is this an old or new packet? */
307 todo_wine ok(flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY, "expect DISCONTINUITY %x\n", flags);
308 ok(pad == next, "GCP %u vs. BufferSize %u\n", (UINT32)pad, next);
309
310 /* Native's position is one period further than what we read.
311 * Perhaps that's precisely the meaning of DATA_DISCONTINUITY:
312 * signal when the position jump left a gap. */
313 todo_wine ok(pos == sum + frames, "Position %u gap %d\n",
314 (UINT)pos, (UINT)pos - sum);
315 if(flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
316 sum = pos;
317 }
318
319 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
320 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
321 sum += frames;
322
323 hr = IAudioClient_GetCurrentPadding(ac, &pad);
324 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
325
326 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
327 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
328
329 trace("Cont'ed position %d pad %u flags %x, amount of frames locked: %u\n",
330 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
331
332 if(hr == S_OK){
333 ok(pos == sum, "Position %u expected %u\n", (UINT)pos, sum);
334 ok(!flags, "flags %u\n", flags);
335
336 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
337 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
338 sum += frames;
339 }
340
341 hr = IAudioClient_Stop(ac);
342 ok(hr == S_OK, "Stop on a started stream returns %08x\n", hr);
343
344 hr = IAudioClient_Start(ac);
345 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
346
347 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
348 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
349
350 hr = IAudioClient_GetCurrentPadding(ac, &pad);
351 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
352
353 trace("Restart position %d pad %u flags %x, amount of frames locked: %u\n",
354 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
355 ok(pad > sum, "restarted GCP %u\n", pad); /* GCP is still near buffer size */
356
357 if(frames){
358 ok(pos == sum, "Position %u expected %u\n", (UINT)pos, sum);
359 ok(!flags, "flags %u\n", flags);
360
361 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
362 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
363 sum += frames;
364 }
365
366 hr = IAudioClient_Stop(ac);
367 ok(hr == S_OK, "Stop on a started stream returns %08x\n", hr);
368
369 hr = IAudioClient_Reset(ac);
370 ok(hr == S_OK, "Reset on a stopped stream returns %08x\n", hr);
371 sum += pad - frames;
372
373 hr = IAudioClient_Start(ac);
374 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
375
376 hr = IAudioClient_GetCurrentPadding(ac, &pad);
377 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
378
379 flags = 0xabadcafe;
380 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
381 ok(hr == AUDCLNT_S_BUFFER_EMPTY || /*PulseAudio*/hr == S_OK,
382 "Initial IAudioCaptureClient_GetBuffer returns %08x\n", hr);
383
384 trace("Reset position %d pad %u flags %x, amount of frames locked: %u\n",
385 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
386
387 if(hr == S_OK){
388 /* Only PulseAudio goes here; despite snd_pcm_drop it manages
389 * to fill GetBufferSize with a single snd_pcm_read */
390 trace("Test marked todo: only PulseAudio gets here\n");
391 todo_wine ok(flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY, "expect DISCONTINUITY %x\n", flags);
392 /* Reset zeroes padding, not the position */
393 ok(pos >= sum, "Position %u last %u\n", (UINT)pos, sum);
394 /*sum = pos; check after next GetBuffer */
395
396 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
397 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
398 sum += frames;
399 }
400 else if(hr == AUDCLNT_S_BUFFER_EMPTY){
401 ok(!pad, "resetted GCP %u\n", pad);
402 Sleep(180);
403 }
404
405 hr = IAudioClient_GetCurrentPadding(ac, &pad);
406 ok(hr == S_OK, "GetCurrentPadding call returns %08x\n", hr);
407
408 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &pos, &qpc);
409 ok(hr == S_OK, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
410 trace("Running position %d pad %u flags %x, amount of frames locked: %u\n",
411 hr==S_OK ? (UINT)pos : -1, pad, flags, frames);
412
413 if(hr == S_OK){
414 /* Some w7 machines signal DATA_DISCONTINUITY here following the
415 * previous AUDCLNT_S_BUFFER_EMPTY, others not. What logic? */
416 ok(pos >= sum, "Position %u gap %d\n", (UINT)pos, (UINT)pos - sum);
417 IAudioCaptureClient_ReleaseBuffer(acc, frames);
418 }
419
420 hr = IAudioClient_Stop(ac);
421 ok(hr == S_OK, "Stop failed: %08x\n", hr);
422
423 ok(ResetEvent(handle), "ResetEvent\n");
424
425 /* Still receiving events! */
426 r = WaitForSingleObject(handle, 20);
427 ok(r == WAIT_OBJECT_0, "Wait(event) after Stop gave %x\n", r);
428
429 hr = IAudioClient_Reset(ac);
430 ok(hr == S_OK, "Reset failed: %08x\n", hr);
431
432 ok(ResetEvent(handle), "ResetEvent\n");
433
434 r = WaitForSingleObject(handle, 120);
435 ok(r == WAIT_OBJECT_0, "Wait(event) after Reset gave %x\n", r);
436
437 hr = IAudioClient_SetEventHandle(ac, NULL);
438 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
439
440 r = WaitForSingleObject(handle, 70);
441 ok(r == WAIT_OBJECT_0, "Wait(NULL event) gave %x\n", r);
442
443 hr = IAudioClient_Start(ac);
444 ok(hr == S_OK, "Start failed: %08x\n", hr);
445
446 IAudioCaptureClient_Release(acc);
447 }
448
449 static void test_audioclient(void)
450 {
451 IAudioClient *ac;
452 IUnknown *unk;
453 HRESULT hr;
454 ULONG ref;
455 WAVEFORMATEX *pwfx, *pwfx2;
456 REFERENCE_TIME t1, t2;
457 HANDLE handle;
458
459 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
460 NULL, (void**)&ac);
461 ok(hr == S_OK, "Activation failed with %08x\n", hr);
462 if(hr != S_OK)
463 return;
464
465 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
466
467 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
468 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
469
470 unk = (void*)(LONG_PTR)0x12345678;
471 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
472 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
473 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
474
475 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
476 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
477 if (unk)
478 {
479 ref = IUnknown_Release(unk);
480 ok(ref == 1, "Released count is %u\n", ref);
481 }
482
483 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
484 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
485 if (unk)
486 {
487 ref = IUnknown_Release(unk);
488 ok(ref == 1, "Released count is %u\n", ref);
489 }
490
491 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
492 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
493
494 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
495 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
496
497 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
498 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
499
500 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
501 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
502 trace("Returned periods: %u.%04u ms %u.%04u ms\n",
503 (UINT)(t1/10000), (UINT)(t1 % 10000),
504 (UINT)(t2/10000), (UINT)(t2 % 10000));
505
506 hr = IAudioClient_GetMixFormat(ac, NULL);
507 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
508
509 hr = IAudioClient_GetMixFormat(ac, &pwfx);
510 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
511
512 if (hr == S_OK)
513 {
514 trace("pwfx: %p\n", pwfx);
515 trace("Tag: %04x\n", pwfx->wFormatTag);
516 trace("bits: %u\n", pwfx->wBitsPerSample);
517 trace("chan: %u\n", pwfx->nChannels);
518 trace("rate: %u\n", pwfx->nSamplesPerSec);
519 trace("align: %u\n", pwfx->nBlockAlign);
520 trace("extra: %u\n", pwfx->cbSize);
521 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
522 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
523 {
524 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
525 trace("Res: %u\n", pwfxe->Samples.wReserved);
526 trace("Mask: %x\n", pwfxe->dwChannelMask);
527 trace("Alg: %s\n",
528 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
529 (IsEqualGUID(&pwfxe->SubFormat,
530 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
531 }
532
533 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
534 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
535 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
536 CoTaskMemFree(pwfx2);
537
538 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
539 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
540
541 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
542 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
543
544 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
545 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
546
547 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
548 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
549 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
550
551 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
552 ok(hr == E_INVALIDARG/*w32*/ ||
553 broken(hr == AUDCLNT_E_UNSUPPORTED_FORMAT/*w64 response from exclusive mode driver */),
554 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
555 }
556
557 test_uninitialized(ac);
558
559 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
560 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
561
562 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
563 ok(hr == E_INVALIDARG || hr == AUDCLNT_E_INVALID_STREAM_FLAG, "Initialize with invalid flags returns %08x\n", hr);
564
565 /* A period != 0 is ignored and the call succeeds.
566 * Since we can only initialize successfully once, skip those tests.
567 */
568 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
569 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
570
571 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 4987654, 0, pwfx, NULL);
572 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
573
574 if (hr != S_OK)
575 {
576 skip("Cannot initialize %08x, remainder of tests is useless\n", hr);
577 CoTaskMemFree(pwfx);
578 return;
579 }
580
581 hr = IAudioClient_GetStreamLatency(ac, NULL);
582 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
583
584 hr = IAudioClient_GetStreamLatency(ac, &t1);
585 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
586 trace("Returned latency: %u.%04u ms\n",
587 (UINT)(t1/10000), (UINT)(t1 % 10000));
588
589 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
590 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
591
592 hr = IAudioClient_SetEventHandle(ac, NULL);
593 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
594
595 hr = IAudioClient_Start(ac);
596 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start before SetEventHandle returns %08x\n", hr);
597
598 hr = IAudioClient_SetEventHandle(ac, handle);
599 ok(hr == S_OK, "SetEventHandle returns %08x\n", hr);
600
601 hr = IAudioClient_Reset(ac);
602 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
603
604 hr = IAudioClient_Stop(ac);
605 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
606
607 hr = IAudioClient_Start(ac);
608 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
609
610 test_capture(ac, handle, pwfx);
611
612 IAudioClient_Release(ac);
613 CloseHandle(handle);
614 CoTaskMemFree(pwfx);
615 }
616
617 static void test_streamvolume(void)
618 {
619 IAudioClient *ac;
620 IAudioStreamVolume *asv;
621 WAVEFORMATEX *fmt;
622 UINT32 chans, i;
623 HRESULT hr;
624 float vol, *vols;
625
626 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
627 NULL, (void**)&ac);
628 ok(hr == S_OK, "Activation failed with %08x\n", hr);
629 if(hr != S_OK)
630 return;
631
632 hr = IAudioClient_GetMixFormat(ac, &fmt);
633 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
634
635 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
636 0, fmt, NULL);
637 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
638 if(hr != S_OK)
639 return;
640
641 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
642 ok(hr == S_OK, "GetService failed: %08x\n", hr);
643
644 hr = IAudioStreamVolume_GetChannelCount(asv, NULL);
645 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
646
647 hr = IAudioStreamVolume_GetChannelCount(asv, &chans);
648 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
649 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
650
651 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, NULL);
652 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
653
654 hr = IAudioStreamVolume_GetChannelVolume(asv, fmt->nChannels, &vol);
655 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
656
657 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, NULL);
658 ok(hr == E_POINTER, "GetChannelCount gave wrong error: %08x\n", hr);
659
660 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
661 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
662 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
663
664 hr = IAudioStreamVolume_SetChannelVolume(asv, fmt->nChannels, -1.f);
665 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
666
667 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, -1.f);
668 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
669
670 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 2.f);
671 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
672
673 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
674 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
675
676 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
677 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
678 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
679
680 hr = IAudioStreamVolume_GetAllVolumes(asv, 0, NULL);
681 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
682
683 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, NULL);
684 ok(hr == E_POINTER, "GetAllVolumes gave wrong error: %08x\n", hr);
685
686 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
687 ok(vols != NULL, "HeapAlloc failed\n");
688
689 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels - 1, vols);
690 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
691
692 hr = IAudioStreamVolume_GetAllVolumes(asv, fmt->nChannels, vols);
693 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
694 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
695 for(i = 1; i < fmt->nChannels; ++i)
696 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
697
698 hr = IAudioStreamVolume_SetAllVolumes(asv, 0, NULL);
699 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
700
701 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, NULL);
702 ok(hr == E_POINTER, "SetAllVolumes gave wrong error: %08x\n", hr);
703
704 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels - 1, vols);
705 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
706
707 hr = IAudioStreamVolume_SetAllVolumes(asv, fmt->nChannels, vols);
708 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
709
710 HeapFree(GetProcessHeap(), 0, vols);
711 IAudioStreamVolume_Release(asv);
712 IAudioClient_Release(ac);
713 CoTaskMemFree(fmt);
714 }
715
716 static void test_channelvolume(void)
717 {
718 IAudioClient *ac;
719 IChannelAudioVolume *acv;
720 WAVEFORMATEX *fmt;
721 UINT32 chans, i;
722 HRESULT hr;
723 float vol, *vols;
724
725 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
726 NULL, (void**)&ac);
727 ok(hr == S_OK, "Activation failed with %08x\n", hr);
728 if(hr != S_OK)
729 return;
730
731 hr = IAudioClient_GetMixFormat(ac, &fmt);
732 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
733
734 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
735 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
736 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
737
738 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&acv);
739 ok(hr == S_OK, "GetService failed: %08x\n", hr);
740 if(hr != S_OK)
741 return;
742
743 hr = IChannelAudioVolume_GetChannelCount(acv, NULL);
744 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
745
746 hr = IChannelAudioVolume_GetChannelCount(acv, &chans);
747 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
748 ok(chans == fmt->nChannels, "GetChannelCount gave wrong number of channels: %d\n", chans);
749
750 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, NULL);
751 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
752
753 hr = IChannelAudioVolume_GetChannelVolume(acv, fmt->nChannels, &vol);
754 ok(hr == E_INVALIDARG, "GetChannelCount gave wrong error: %08x\n", hr);
755
756 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, NULL);
757 ok(hr == NULL_PTR_ERR, "GetChannelCount gave wrong error: %08x\n", hr);
758
759 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
760 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
761 ok(vol == 1.f, "Channel volume was not 1: %f\n", vol);
762
763 hr = IChannelAudioVolume_SetChannelVolume(acv, fmt->nChannels, -1.f, NULL);
764 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
765
766 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, -1.f, NULL);
767 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
768
769 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 2.f, NULL);
770 ok(hr == E_INVALIDARG, "SetChannelVolume gave wrong error: %08x\n", hr);
771
772 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 0.2f, NULL);
773 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
774
775 hr = IChannelAudioVolume_GetChannelVolume(acv, 0, &vol);
776 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
777 ok(fabsf(vol - 0.2f) < 0.05f, "Channel volume wasn't 0.2: %f\n", vol);
778
779 hr = IChannelAudioVolume_GetAllVolumes(acv, 0, NULL);
780 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
781
782 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, NULL);
783 ok(hr == NULL_PTR_ERR, "GetAllVolumes gave wrong error: %08x\n", hr);
784
785 vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
786 ok(vols != NULL, "HeapAlloc failed\n");
787
788 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels - 1, vols);
789 ok(hr == E_INVALIDARG, "GetAllVolumes gave wrong error: %08x\n", hr);
790
791 hr = IChannelAudioVolume_GetAllVolumes(acv, fmt->nChannels, vols);
792 ok(hr == S_OK, "GetAllVolumes failed: %08x\n", hr);
793 ok(fabsf(vols[0] - 0.2f) < 0.05f, "Channel 0 volume wasn't 0.2: %f\n", vol);
794 for(i = 1; i < fmt->nChannels; ++i)
795 ok(vols[i] == 1.f, "Channel %d volume is not 1: %f\n", i, vols[i]);
796
797 hr = IChannelAudioVolume_SetAllVolumes(acv, 0, NULL, NULL);
798 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
799
800 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, NULL, NULL);
801 ok(hr == NULL_PTR_ERR, "SetAllVolumes gave wrong error: %08x\n", hr);
802
803 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels - 1, vols, NULL);
804 ok(hr == E_INVALIDARG, "SetAllVolumes gave wrong error: %08x\n", hr);
805
806 hr = IChannelAudioVolume_SetAllVolumes(acv, fmt->nChannels, vols, NULL);
807 ok(hr == S_OK, "SetAllVolumes failed: %08x\n", hr);
808
809 hr = IChannelAudioVolume_SetChannelVolume(acv, 0, 1.0f, NULL);
810 ok(hr == S_OK, "SetChannelVolume failed: %08x\n", hr);
811
812 HeapFree(GetProcessHeap(), 0, vols);
813 IChannelAudioVolume_Release(acv);
814 IAudioClient_Release(ac);
815 CoTaskMemFree(fmt);
816 }
817
818 static void test_simplevolume(void)
819 {
820 IAudioClient *ac;
821 ISimpleAudioVolume *sav;
822 WAVEFORMATEX *fmt;
823 HRESULT hr;
824 float vol;
825 BOOL mute;
826
827 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
828 NULL, (void**)&ac);
829 ok(hr == S_OK, "Activation failed with %08x\n", hr);
830 if(hr != S_OK)
831 return;
832
833 hr = IAudioClient_GetMixFormat(ac, &fmt);
834 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
835
836 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
837 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, NULL);
838 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
839
840 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
841 ok(hr == S_OK, "GetService failed: %08x\n", hr);
842 if(hr != S_OK)
843 return;
844
845 hr = ISimpleAudioVolume_GetMasterVolume(sav, NULL);
846 ok(hr == NULL_PTR_ERR, "GetMasterVolume gave wrong error: %08x\n", hr);
847
848 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
849 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
850 ok(vol == 1.f, "Master volume wasn't 1: %f\n", vol);
851
852 hr = ISimpleAudioVolume_SetMasterVolume(sav, -1.f, NULL);
853 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
854
855 hr = ISimpleAudioVolume_SetMasterVolume(sav, 2.f, NULL);
856 ok(hr == E_INVALIDARG, "SetMasterVolume gave wrong error: %08x\n", hr);
857
858 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.2f, NULL);
859 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
860
861 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
862 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
863 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
864
865 hr = ISimpleAudioVolume_GetMute(sav, NULL);
866 ok(hr == NULL_PTR_ERR, "GetMute gave wrong error: %08x\n", hr);
867
868 mute = TRUE;
869 hr = ISimpleAudioVolume_GetMute(sav, &mute);
870 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
871 ok(mute == FALSE, "Session is already muted\n");
872
873 hr = ISimpleAudioVolume_SetMute(sav, TRUE, NULL);
874 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
875
876 mute = FALSE;
877 hr = ISimpleAudioVolume_GetMute(sav, &mute);
878 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
879 ok(mute == TRUE, "Session should have been muted\n");
880
881 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
882 ok(hr == S_OK, "GetMasterVolume failed: %08x\n", hr);
883 ok(fabsf(vol - 0.2f) < 0.05f, "Master volume wasn't 0.2: %f\n", vol);
884
885 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
886 ok(hr == S_OK, "SetMasterVolume failed: %08x\n", hr);
887
888 mute = FALSE;
889 hr = ISimpleAudioVolume_GetMute(sav, &mute);
890 ok(hr == S_OK, "GetMute failed: %08x\n", hr);
891 ok(mute == TRUE, "Session should have been muted\n");
892
893 hr = ISimpleAudioVolume_SetMute(sav, FALSE, NULL);
894 ok(hr == S_OK, "SetMute failed: %08x\n", hr);
895
896 ISimpleAudioVolume_Release(sav);
897 IAudioClient_Release(ac);
898 CoTaskMemFree(fmt);
899 }
900
901 static void test_volume_dependence(void)
902 {
903 IAudioClient *ac, *ac2;
904 ISimpleAudioVolume *sav;
905 IChannelAudioVolume *cav;
906 IAudioStreamVolume *asv;
907 WAVEFORMATEX *fmt;
908 HRESULT hr;
909 float vol;
910 GUID session;
911 UINT32 nch;
912
913 hr = CoCreateGuid(&session);
914 ok(hr == S_OK, "CoCreateGuid failed: %08x\n", hr);
915
916 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
917 NULL, (void**)&ac);
918 ok(hr == S_OK, "Activation failed with %08x\n", hr);
919 if(hr != S_OK)
920 return;
921
922 hr = IAudioClient_GetMixFormat(ac, &fmt);
923 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
924
925 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
926 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
927 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
928
929 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
930 ok(hr == S_OK, "GetService (SimpleAudioVolume) failed: %08x\n", hr);
931
932 hr = IAudioClient_GetService(ac, &IID_IChannelAudioVolume, (void**)&cav);
933 ok(hr == S_OK, "GetService (ChannelAudioVolme) failed: %08x\n", hr);
934
935 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&asv);
936 ok(hr == S_OK, "GetService (AudioStreamVolume) failed: %08x\n", hr);
937 if(hr != S_OK)
938 return;
939
940 hr = IAudioStreamVolume_SetChannelVolume(asv, 0, 0.2f);
941 ok(hr == S_OK, "ASV_SetChannelVolume failed: %08x\n", hr);
942
943 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 0.4f, NULL);
944 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
945
946 hr = ISimpleAudioVolume_SetMasterVolume(sav, 0.6f, NULL);
947 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
948
949 hr = IAudioStreamVolume_GetChannelVolume(asv, 0, &vol);
950 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
951 ok(fabsf(vol - 0.2f) < 0.05f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
952
953 hr = IChannelAudioVolume_GetChannelVolume(cav, 0, &vol);
954 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
955 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
956
957 hr = ISimpleAudioVolume_GetMasterVolume(sav, &vol);
958 ok(hr == S_OK, "SAV_GetMasterVolume failed: %08x\n", hr);
959 ok(fabsf(vol - 0.6f) < 0.05f, "SAV_GetMasterVolume gave wrong volume: %f\n", vol);
960
961 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
962 NULL, (void**)&ac2);
963 if(SUCCEEDED(hr)){
964 IChannelAudioVolume *cav2;
965 IAudioStreamVolume *asv2;
966
967 hr = IAudioClient_Initialize(ac2, AUDCLNT_SHAREMODE_SHARED,
968 AUDCLNT_STREAMFLAGS_NOPERSIST, 5000000, 0, fmt, &session);
969 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
970
971 hr = IAudioClient_GetService(ac2, &IID_IChannelAudioVolume, (void**)&cav2);
972 ok(hr == S_OK, "GetService failed: %08x\n", hr);
973
974 hr = IAudioClient_GetService(ac2, &IID_IAudioStreamVolume, (void**)&asv2);
975 ok(hr == S_OK, "GetService failed: %08x\n", hr);
976
977 hr = IChannelAudioVolume_GetChannelVolume(cav2, 0, &vol);
978 ok(hr == S_OK, "CAV_GetChannelVolume failed: %08x\n", hr);
979 ok(fabsf(vol - 0.4f) < 0.05f, "CAV_GetChannelVolume gave wrong volume: %f\n", vol);
980
981 hr = IAudioStreamVolume_GetChannelVolume(asv2, 0, &vol);
982 ok(hr == S_OK, "ASV_GetChannelVolume failed: %08x\n", hr);
983 ok(vol == 1.f, "ASV_GetChannelVolume gave wrong volume: %f\n", vol);
984
985 hr = IChannelAudioVolume_GetChannelCount(cav2, &nch);
986 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
987 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
988
989 hr = IAudioStreamVolume_GetChannelCount(asv2, &nch);
990 ok(hr == S_OK, "GetChannelCount failed: %08x\n", hr);
991 ok(nch == fmt->nChannels, "Got wrong channel count, expected %u: %u\n", fmt->nChannels, nch);
992
993 IAudioStreamVolume_Release(asv2);
994 IChannelAudioVolume_Release(cav2);
995 IAudioClient_Release(ac2);
996 }else
997 skip("Unable to open the same device twice. Skipping session volume control tests\n");
998
999 hr = IChannelAudioVolume_SetChannelVolume(cav, 0, 1.f, NULL);
1000 ok(hr == S_OK, "CAV_SetChannelVolume failed: %08x\n", hr);
1001
1002 hr = ISimpleAudioVolume_SetMasterVolume(sav, 1.f, NULL);
1003 ok(hr == S_OK, "SAV_SetMasterVolume failed: %08x\n", hr);
1004
1005 CoTaskMemFree(fmt);
1006 ISimpleAudioVolume_Release(sav);
1007 IChannelAudioVolume_Release(cav);
1008 IAudioStreamVolume_Release(asv);
1009 IAudioClient_Release(ac);
1010 }
1011
1012 static void test_marshal(void)
1013 {
1014 IStream *pStream;
1015 IAudioClient *ac, *acDest;
1016 IAudioCaptureClient *cc, *ccDest;
1017 WAVEFORMATEX *pwfx;
1018 HRESULT hr;
1019
1020 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
1021 NULL, (void**)&ac);
1022 ok(hr == S_OK, "Activation failed with %08x\n", hr);
1023 if(hr != S_OK)
1024 return;
1025
1026 hr = IAudioClient_GetMixFormat(ac, &pwfx);
1027 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
1028
1029 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
1030 0, pwfx, NULL);
1031 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
1032
1033 CoTaskMemFree(pwfx);
1034
1035 hr = IAudioClient_GetService(ac, &IID_IAudioCaptureClient, (void**)&cc);
1036 ok(hr == S_OK, "GetService failed: %08x\n", hr);
1037 if(hr != S_OK) {
1038 IAudioClient_Release(ac);
1039 return;
1040 }
1041
1042 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1043 ok(hr == S_OK, "CreateStreamOnHGlobal failed 0x%08x\n", hr);
1044
1045 /* marshal IAudioClient */
1046
1047 hr = CoMarshalInterface(pStream, &IID_IAudioClient, (IUnknown*)ac, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1048 ok(hr == S_OK, "CoMarshalInterface IAudioClient failed 0x%08x\n", hr);
1049
1050 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1051 hr = CoUnmarshalInterface(pStream, &IID_IAudioClient, (void **)&acDest);
1052 ok(hr == S_OK, "CoUnmarshalInterface IAudioClient failed 0x%08x\n", hr);
1053 if (hr == S_OK)
1054 IAudioClient_Release(acDest);
1055
1056 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1057 /* marshal IAudioCaptureClient */
1058
1059 hr = CoMarshalInterface(pStream, &IID_IAudioCaptureClient, (IUnknown*)cc, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1060 ok(hr == S_OK, "CoMarshalInterface IAudioCaptureClient failed 0x%08x\n", hr);
1061
1062 IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
1063 hr = CoUnmarshalInterface(pStream, &IID_IAudioCaptureClient, (void **)&ccDest);
1064 ok(hr == S_OK, "CoUnmarshalInterface IAudioCaptureClient failed 0x%08x\n", hr);
1065 if (hr == S_OK)
1066 IAudioCaptureClient_Release(ccDest);
1067
1068 IStream_Release(pStream);
1069
1070 IAudioClient_Release(ac);
1071 IAudioCaptureClient_Release(cc);
1072
1073 }
1074
1075 START_TEST(capture)
1076 {
1077 HRESULT hr;
1078 IMMDeviceEnumerator *mme = NULL;
1079
1080 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1081 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
1082 if (FAILED(hr))
1083 {
1084 skip("mmdevapi not available: 0x%08x\n", hr);
1085 goto cleanup;
1086 }
1087
1088 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture, eMultimedia, &dev);
1089 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
1090 if (hr != S_OK || !dev)
1091 {
1092 if (hr == E_NOTFOUND)
1093 skip("No sound card available\n");
1094 else
1095 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
1096 goto cleanup;
1097 }
1098
1099 test_audioclient();
1100 test_streamvolume();
1101 test_channelvolume();
1102 test_simplevolume();
1103 test_volume_dependence();
1104 test_marshal();
1105
1106 IMMDevice_Release(dev);
1107
1108 cleanup:
1109 if (mme)
1110 IMMDeviceEnumerator_Release(mme);
1111 CoUninitialize();
1112 }