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 playback specific mechanisms
21 * - IAudioClient with eRender and IAudioRenderClient
24 #include "wine/test.h"
34 #include "mmdeviceapi.h"
35 #include "audioclient.h"
37 static IMMDevice *dev = NULL;
39 static void test_uninitialized(IAudioClient *ac)
45 HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
48 hr = IAudioClient_GetBufferSize(ac, &num);
49 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
51 hr = IAudioClient_GetStreamLatency(ac, &t1);
52 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
54 hr = IAudioClient_GetCurrentPadding(ac, &num);
55 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
57 hr = IAudioClient_Start(ac);
58 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
60 hr = IAudioClient_Stop(ac);
61 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
63 hr = IAudioClient_Reset(ac);
64 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
66 hr = IAudioClient_SetEventHandle(ac, handle);
67 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
69 hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
70 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
75 static void test_audioclient(void)
81 WAVEFORMATEX *pwfx, *pwfx2;
82 REFERENCE_TIME t1, t2;
85 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
87 ok(hr == S_OK, "Activation failed with %08x\n", hr);
91 handle = CreateEventW(NULL, FALSE, FALSE, NULL);
93 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
94 ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
96 unk = (void*)(LONG_PTR)0x12345678;
97 hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
98 ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
99 ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
101 hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
102 ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
105 ref = IUnknown_Release(unk);
106 ok(ref == 1, "Released count is %u\n", ref);
109 hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
110 ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
113 ref = IUnknown_Release(unk);
114 ok(ref == 1, "Released count is %u\n", ref);
117 hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
118 ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
120 hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
121 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
123 hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
124 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
126 hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
127 ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
128 trace("Returned periods: %u.%05u ms %u.%05u ms\n",
129 (UINT)(t1/10000), (UINT)(t1 % 10000),
130 (UINT)(t2/10000), (UINT)(t2 % 10000));
132 hr = IAudioClient_GetMixFormat(ac, NULL);
133 ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
135 hr = IAudioClient_GetMixFormat(ac, &pwfx);
136 ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
140 trace("pwfx: %p\n", pwfx);
141 trace("Tag: %04x\n", pwfx->wFormatTag);
142 trace("bits: %u\n", pwfx->wBitsPerSample);
143 trace("chan: %u\n", pwfx->nChannels);
144 trace("rate: %u\n", pwfx->nSamplesPerSec);
145 trace("align: %u\n", pwfx->nBlockAlign);
146 trace("extra: %u\n", pwfx->cbSize);
147 ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
148 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
150 WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
151 trace("Res: %u\n", pwfxe->Samples.wReserved);
152 trace("Mask: %x\n", pwfxe->dwChannelMask);
154 IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
155 (IsEqualGUID(&pwfxe->SubFormat,
156 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
159 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
160 ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
161 ok(pwfx2 == NULL, "pwfx2 is non-null\n");
162 CoTaskMemFree(pwfx2);
164 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
165 ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
167 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
168 ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
170 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
171 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
173 hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
174 ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
175 ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
177 hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
178 ok(hr == E_INVALIDARG ||
179 hr == AUDCLNT_E_UNSUPPORTED_FORMAT,
180 "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
183 test_uninitialized(ac);
185 hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
186 ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
188 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
189 ok(hr == E_INVALIDARG, "Initialize with invalid flags returns %08x\n", hr);
191 /* It seems that if length > 2s or periodicity != 0 the length is ignored and call succeeds
192 * Since we can only initialize successfully once, skip those tests.
194 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
195 ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
197 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
198 ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
202 skip("Cannot initialize %08x, remainder of tests is useless\n", hr);
207 hr = IAudioClient_GetStreamLatency(ac, NULL);
208 ok(hr == E_POINTER, "GetStreamLatency(NULL) call returns %08x\n", hr);
210 hr = IAudioClient_GetStreamLatency(ac, &t1);
211 ok(hr == S_OK, "Valid GetStreamLatency call returns %08x\n", hr);
212 trace("Returned latency: %u.%05u ms\n",
213 (UINT)(t1/10000), (UINT)(t1 % 10000));
215 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
216 ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
218 hr = IAudioClient_SetEventHandle(ac, NULL);
219 ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
221 hr = IAudioClient_SetEventHandle(ac, handle);
222 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
223 broken(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)) ||
224 broken(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) /* Some 2k8 */ ||
225 broken(hr == HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME)) /* Some Vista */
226 , "SetEventHandle returns %08x\n", hr);
228 hr = IAudioClient_Reset(ac);
229 ok(hr == S_OK, "Reset on a resetted stream returns %08x\n", hr);
231 hr = IAudioClient_Stop(ac);
232 ok(hr == S_FALSE, "Stop on a stopped stream returns %08x\n", hr);
234 hr = IAudioClient_Start(ac);
235 ok(hr == S_OK, "Start on a stopped stream returns %08x\n", hr);
237 IAudioClient_Release(ac);
243 static void test_references(void)
246 IAudioRenderClient *rc;
247 ISimpleAudioVolume *sav;
253 /* IAudioRenderClient */
254 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
256 ok(hr == S_OK, "Activation failed with %08x\n", hr);
260 hr = IAudioClient_GetMixFormat(ac, &pwfx);
261 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
265 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
267 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
271 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&rc);
272 ok(hr == S_OK, "GetService failed: %08x\n", hr);
274 IAudioRenderClient_AddRef(rc);
275 ref = IAudioRenderClient_Release(rc);
276 ok(ref != 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
278 ref = IAudioClient_Release(ac);
279 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
281 ref = IAudioRenderClient_Release(rc);
282 ok(ref == 0, "RenderClient_Release gave wrong refcount: %u\n", ref);
284 /* ISimpleAudioVolume */
285 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
287 ok(hr == S_OK, "Activation failed with %08x\n", hr);
291 hr = IAudioClient_GetMixFormat(ac, &pwfx);
292 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
294 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
296 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
300 hr = IAudioClient_GetService(ac, &IID_ISimpleAudioVolume, (void**)&sav);
301 ok(hr == S_OK, "GetService failed: %08x\n", hr);
303 ISimpleAudioVolume_AddRef(sav);
304 ref = ISimpleAudioVolume_Release(sav);
305 ok(ref != 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
307 ref = IAudioClient_Release(ac);
308 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
310 ref = ISimpleAudioVolume_Release(sav);
311 ok(ref == 0, "SimpleAudioVolume_Release gave wrong refcount: %u\n", ref);
314 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
316 ok(hr == S_OK, "Activation failed with %08x\n", hr);
320 hr = IAudioClient_GetMixFormat(ac, &pwfx);
321 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
323 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000,
325 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
329 hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl);
330 ok(hr == S_OK, "GetService failed: %08x\n", hr);
332 IAudioClock_AddRef(acl);
333 ref = IAudioClock_Release(acl);
334 ok(ref != 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
336 ref = IAudioClient_Release(ac);
337 ok(ref != 0, "Client_Release gave wrong refcount: %u\n", ref);
339 ref = IAudioClock_Release(acl);
340 ok(ref == 0, "AudioClock_Release gave wrong refcount: %u\n", ref);
343 static void test_event(void)
350 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
352 ok(hr == S_OK, "Activation failed with %08x\n", hr);
356 hr = IAudioClient_GetMixFormat(ac, &pwfx);
357 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
361 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
362 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 5000000,
364 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
368 event = CreateEventW(NULL, FALSE, FALSE, NULL);
369 ok(event != NULL, "CreateEvent failed\n");
371 hr = IAudioClient_Start(ac);
372 ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_SET, "Start failed: %08x\n", hr);
374 hr = IAudioClient_SetEventHandle(ac, event);
375 ok(hr == S_OK, "SetEventHandle failed: %08x\n", hr);
377 hr = IAudioClient_Start(ac);
378 ok(hr == S_OK, "Start failed: %08x\n", hr);
380 hr = IAudioClient_Stop(ac);
381 ok(hr == S_OK, "Start failed: %08x\n", hr);
383 /* test releasing a playing stream */
384 hr = IAudioClient_Start(ac);
385 ok(hr == S_OK, "Start failed: %08x\n", hr);
386 IAudioClient_Release(ac);
391 static void test_padding(void)
395 IAudioRenderClient *arc;
397 REFERENCE_TIME minp, defp;
399 UINT32 psize, pad, written;
401 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER,
403 ok(hr == S_OK, "Activation failed with %08x\n", hr);
407 hr = IAudioClient_GetMixFormat(ac, &pwfx);
408 ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr);
412 hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED,
413 0, 5000000, 0, pwfx, NULL);
414 ok(hr == S_OK, "Initialize failed: %08x\n", hr);
416 hr = IAudioClient_GetDevicePeriod(ac, &defp, &minp);
417 ok(hr == S_OK, "GetDevicePeriod failed: %08x\n", hr);
418 ok(defp != 0, "Default period is 0\n");
419 ok(minp != 0, "Minimum period is 0\n");
420 ok(minp <= defp, "Mininum period is greater than default period\n");
422 hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc);
423 ok(hr == S_OK, "GetService failed: %08x\n", hr);
425 psize = (defp / 10000000.) * pwfx->nSamplesPerSec * pwfx->nBlockAlign;
428 hr = IAudioClient_GetCurrentPadding(ac, &pad);
429 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
430 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
432 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
433 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
434 ok(buf != NULL, "NULL buffer returned\n");
436 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
437 AUDCLNT_BUFFERFLAGS_SILENT);
438 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
441 hr = IAudioClient_GetCurrentPadding(ac, &pad);
442 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
443 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
445 psize = (minp / 10000000.) * pwfx->nSamplesPerSec * pwfx->nBlockAlign;
447 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
448 ok(hr == S_OK, "GetBuffer failed: %08x\n", hr);
449 ok(buf != NULL, "NULL buffer returned\n");
451 hr = IAudioRenderClient_ReleaseBuffer(arc, psize,
452 AUDCLNT_BUFFERFLAGS_SILENT);
453 ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr);
456 hr = IAudioClient_GetCurrentPadding(ac, &pad);
457 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
458 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
460 /* overfull buffer. requested 1/2s buffer size, so try
461 * to get a 1/2s buffer, which should fail */
462 psize = pwfx->nSamplesPerSec / 2.;
463 hr = IAudioRenderClient_GetBuffer(arc, psize, &buf);
464 ok(hr == AUDCLNT_E_BUFFER_TOO_LARGE, "GetBuffer gave wrong error: %08x\n", hr);
466 hr = IAudioRenderClient_ReleaseBuffer(arc, psize, 0);
467 ok(hr == AUDCLNT_E_OUT_OF_ORDER, "ReleaseBuffer gave wrong error: %08x\n", hr);
469 hr = IAudioClient_GetCurrentPadding(ac, &pad);
470 ok(hr == S_OK, "GetCurrentPadding failed: %08x\n", hr);
471 ok(pad == written, "GetCurrentPadding returned %u, should be %u\n", pad, written);
475 IAudioRenderClient_Release(arc);
476 IAudioClient_Release(ac);
482 IMMDeviceEnumerator *mme = NULL;
484 CoInitializeEx(NULL, COINIT_MULTITHREADED);
485 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
488 skip("mmdevapi not available: 0x%08x\n", hr);
492 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
493 ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
494 if (hr != S_OK || !dev)
496 if (hr == E_NOTFOUND)
497 skip("No sound card available\n");
499 skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
508 IMMDevice_Release(dev);
512 IMMDeviceEnumerator_Release(mme);