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