2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
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.
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.
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
19 /* This test is for audio capture specific mechanisms
21 * - IAudioClient with eCapture and IAudioCaptureClient
24 #include "wine/test.h"
34 #include "mmdeviceapi.h"
35 #include "audioclient.h"
37 static void test_uninitialized(IAudioClient *ac)
43 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
46 hr = IAudioClient_GetBufferSize(ac, &num);
47 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
49 hr = IAudioClient_GetStreamLatency(ac, &t1);
50 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
52 hr = IAudioClient_GetCurrentPadding(ac, &num);
53 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
55 hr = IAudioClient_Start(ac);
56 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
58 hr = IAudioClient_Stop(ac);
59 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
61 hr = IAudioClient_Reset(ac);
62 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
64 hr = IAudioClient_SetEventHandle(ac, handle);
65 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
67 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
68 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
73 static void test_capture(IAudioClient *ac, HANDLE handle, WAVEFORMATEX *wfx)
75 IAudioCaptureClient *acc;
80 UINT64 devpos, qpcpos;
82 hr = IAudioClient_GetService(ac, &IID_IAudioCaptureClient, (void**)&acc);
83 ok(hr == S_OK, "IAudioClient_GetService(IID_IAudioCaptureClient) returns %08x\n", hr);
87 hr = IAudioCaptureClient_GetNextPacketSize(acc, NULL);
88 ok(hr == E_POINTER, "IAudioCaptureClient_GetNextPacketSize(NULL) returns %08x\n", hr);
90 ok(WaitForSingleObject(handle, 2000) == WAIT_OBJECT_0, "Waiting on event handle failed!\n");
92 /* frames can be 0 if no data is available yet.. */
93 hr = IAudioCaptureClient_GetNextPacketSize(acc, &frames);
94 ok(hr == S_OK, "IAudioCaptureClient_GetNextPacketSize returns %08x\n", hr);
96 data = (BYTE*)(DWORD_PTR)0xdeadbeef;
97 hr = IAudioCaptureClient_GetBuffer(acc, &data, NULL, NULL, NULL, NULL);
98 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(data, NULL, NULL) returns %08x\n", hr);
99 ok((DWORD_PTR)data == 0xdeadbeef, "data is reset to %p\n", data);
102 hr = IAudioCaptureClient_GetBuffer(acc, NULL, &frames, NULL, NULL, NULL);
103 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(NULL, &frames, NULL) returns %08x\n", hr);
104 ok(frames == 0xdeadbeef, "frames is reset to %08x\n", frames);
107 hr = IAudioCaptureClient_GetBuffer(acc, NULL, NULL, &flags, NULL, NULL);
108 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(NULL, NULL, &flags) returns %08x\n", hr);
109 ok(flags == 0xdeadbeef, "flags is reset to %08x\n", flags);
111 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, NULL, NULL, NULL);
112 ok(hr == E_POINTER, "IAudioCaptureClient_GetBuffer(&ata, &frames, NULL) returns %08x\n", hr);
114 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &devpos, &qpcpos);
115 ok(hr == S_OK || hr == AUDCLNT_S_BUFFER_EMPTY, "Valid IAudioCaptureClient_GetBuffer returns %08x\n", hr);
117 ok(frames, "Amount of frames locked is 0!\n");
118 else if (hr == AUDCLNT_S_BUFFER_EMPTY)
119 ok(!frames, "Amount of frames locked with empty buffer is %u!\n", frames);
121 ok(0, "GetBuffer returned %08x\n", hr);
122 trace("Device position is at %u, amount of frames locked: %u\n", (DWORD)devpos, frames);
125 hr = IAudioCaptureClient_GetBuffer(acc, &data, &frames, &flags, &devpos, &qpcpos);
126 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "Out of order IAudioCaptureClient_GetBuffer returns %08x\n", hr);
129 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
130 ok(hr == S_OK, "Releasing buffer returns %08x\n", hr);
133 hr = IAudioCaptureClient_ReleaseBuffer(acc, frames);
134 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "Releasing buffer twice returns %08x\n", hr);
137 IUnknown_Release(acc);
140 static void test_audioclient(IAudioClient *ac)
145 WAVEFORMATEX *pwfx, *pwfx2;
146 REFERENCE_TIME t1, t2;
148 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
150 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
151 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
153 unk = (void*)(LONG_PTR)0x12345678;
154 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
155 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
156 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
158 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
159 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
162 ref = IUnknown_Release(unk);
163 ok(ref == 1, "Released count is %u\n", ref);
166 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
167 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
170 ref = IUnknown_Release(unk);
171 ok(ref == 1, "Released count is %u\n", ref);
174 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
175 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
177 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
178 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
180 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
181 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
183 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
184 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
185 trace("Returned periods: %u.%05u ms %u.%05u ms\n",
186 (UINT)(t1/10000), (UINT)(t1 % 10000),
187 (UINT)(t2/10000), (UINT)(t2 % 10000));
189 hr = IAudioClient_GetMixFormat(ac, NULL);
190 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
192 hr = IAudioClient_GetMixFormat(ac, &pwfx);
193 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
197 trace("pwfx: %p\n", pwfx);
198 trace("Tag: %04x\n", pwfx->wFormatTag);
199 trace("bits: %u\n", pwfx->wBitsPerSample);
200 trace("chan: %u\n", pwfx->nChannels);
201 trace("rate: %u\n", pwfx->nSamplesPerSec);
202 trace("align: %u\n", pwfx->nBlockAlign);
203 trace("extra: %u\n", pwfx->cbSize);
204 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
205 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
207 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
208 trace("Res: %u\n", pwfxe->Samples.wReserved);
209 trace("Mask: %x\n", pwfxe->dwChannelMask);
211 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
212 (IsEqualGUID(&pwfxe->SubFormat,
213 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
216 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
217 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
218 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
219 CoTaskMemFree(pwfx2);
221 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
222 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
224 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
225 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
227 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
228 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
230 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
231 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
232 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
234 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
235 ok(hr == E_INVALIDARG, "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
238 test_uninitialized(ac);
240 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
241 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
243 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
244 ok(hr == E_INVALIDARG, "Initialize with invalid flags returns %08x\n", hr);
246 /* It seems that if length > 2s or periodicity != 0 the length is ignored and call succeeds
247 * Since we can only initialize successfully once skip those tests
249 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
250 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
252 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000, 0, pwfx, NULL);
253 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
257 skip("Cannot initialize %08x, remainder of tests is useless\n", hr);
262 hr = IAudioClient_GetStreamLatency(ac, NULL);
263 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
265 hr = IAudioClient_GetStreamLatency(ac, &t1);
266 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
267 trace("Returned latency: %u.%05u ms\n",
268 (UINT)(t1/10000), (UINT)(t1 % 10000));
270 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
271 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
273 hr = IAudioClient_SetEventHandle(ac, NULL);
274 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
276 hr = IAudioClient_Start(ac);
277 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start before SetEventHandle returns %08x\n", hr);
279 hr = IAudioClient_SetEventHandle(ac, handle);
280 ok(hr == S_OK, "SetEventHandle returns %08x\n", hr);
282 hr = IAudioClient_Reset(ac);
283 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
285 hr = IAudioClient_Stop(ac);
286 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
288 hr = IAudioClient_Start(ac);
289 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
291 test_capture(ac, handle, pwfx);
300 IMMDeviceEnumerator *mme = NULL;
301 IMMDevice *dev = NULL;
302 IAudioClient *ac = NULL;
304 CoInitializeEx(NULL, COINIT_MULTITHREADED);
305 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
308 skip("mmdevapi not available: 0x%08x\n", hr);
312 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eCapture, eMultimedia, &dev);
313 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
314 if (hr != S_OK || !dev)
316 if (hr == E_NOTFOUND)
317 skip("No sound card available\n");
319 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
323 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, (void**)&ac);
324 ok(hr == S_OK, "Activation failed with %08x\n", hr);
327 test_audioclient(ac);
328 IAudioClient_Release(ac);
330 IMMDevice_Release(dev);
334 IMMDeviceEnumerator_Release(mme);