mmdevapi/tests: Fix a failing test for Vista/W2k8.
[wine] / dlls / mmdevapi / tests / render.c
1 /*
2  * Copyright 2010 Maarten Lankhorst for Codeweavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 /* This test is for audio playback specific mechanisms
20  * Tests:
21  * - IAudioClient with eRender and IAudioRenderClient
22  */
23
24 #include "wine/test.h"
25
26 #define CINTERFACE
27 #define COBJMACROS
28
29 #ifdef STANDALONE
30 #include "initguid.h"
31 #endif
32
33 #include "unknwn.h"
34 #include "uuids.h"
35 #include "mmdeviceapi.h"
36 #include "audioclient.h"
37
38 static void test_uninitialized(IAudioClient *ac)
39 {
40     HRESULT hr;
41     UINT32 num;
42     REFERENCE_TIME t1;
43
44     HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
45     IUnknown *unk;
46
47     hr = IAudioClient_GetBufferSize(ac, &num);
48     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetBufferSize call returns %08x\n", hr);
49
50     hr = IAudioClient_GetStreamLatency(ac, &t1);
51     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetStreamLatency call returns %08x\n", hr);
52
53     hr = IAudioClient_GetCurrentPadding(ac, &num);
54     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetCurrentPadding call returns %08x\n", hr);
55
56     hr = IAudioClient_Start(ac);
57     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Start call returns %08x\n", hr);
58
59     hr = IAudioClient_Stop(ac);
60     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Stop call returns %08x\n", hr);
61
62     hr = IAudioClient_Reset(ac);
63     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized Reset call returns %08x\n", hr);
64
65     hr = IAudioClient_SetEventHandle(ac, handle);
66     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized SetEventHandle call returns %08x\n", hr);
67
68     hr = IAudioClient_GetService(ac, &IID_IAudioStreamVolume, (void**)&unk);
69     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Uninitialized GetService call returns %08x\n", hr);
70
71     CloseHandle(handle);
72 }
73
74 static void test_audioclient(IAudioClient *ac)
75 {
76     IUnknown *unk;
77     HRESULT hr;
78     ULONG ref;
79     WAVEFORMATEX *pwfx, *pwfx2;
80     REFERENCE_TIME t1, t2;
81
82     HANDLE handle = CreateEventW(NULL, FALSE, FALSE, NULL);
83
84     hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, NULL);
85     ok(hr == E_POINTER, "QueryInterface(NULL) returned %08x\n", hr);
86
87     unk = (void*)(LONG_PTR)0x12345678;
88     hr = IAudioClient_QueryInterface(ac, &IID_NULL, (void**)&unk);
89     ok(hr == E_NOINTERFACE, "QueryInterface(IID_NULL) returned %08x\n", hr);
90     ok(!unk, "QueryInterface(IID_NULL) returned non-null pointer %p\n", unk);
91
92     hr = IAudioClient_QueryInterface(ac, &IID_IUnknown, (void**)&unk);
93     ok(hr == S_OK, "QueryInterface(IID_IUnknown) returned %08x\n", hr);
94     if (unk)
95     {
96         ref = IUnknown_Release(unk);
97         ok(ref == 1, "Released count is %u\n", ref);
98     }
99
100     hr = IAudioClient_QueryInterface(ac, &IID_IAudioClient, (void**)&unk);
101     ok(hr == S_OK, "QueryInterface(IID_IAudioClient) returned %08x\n", hr);
102     if (unk)
103     {
104         ref = IUnknown_Release(unk);
105         ok(ref == 1, "Released count is %u\n", ref);
106     }
107
108     hr = IAudioClient_GetDevicePeriod(ac, NULL, NULL);
109     ok(hr == E_POINTER, "Invalid GetDevicePeriod call returns %08x\n", hr);
110
111     hr = IAudioClient_GetDevicePeriod(ac, &t1, NULL);
112     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
113
114     hr = IAudioClient_GetDevicePeriod(ac, NULL, &t2);
115     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
116
117     hr = IAudioClient_GetDevicePeriod(ac, &t1, &t2);
118     ok(hr == S_OK, "Valid GetDevicePeriod call returns %08x\n", hr);
119     trace("Returned periods: %u.%05u ms %u.%05u ms\n",
120           (UINT)(t1/10000), (UINT)(t1 % 10000),
121           (UINT)(t2/10000), (UINT)(t2 % 10000));
122
123     hr = IAudioClient_GetMixFormat(ac, NULL);
124     ok(hr == E_POINTER, "GetMixFormat returns %08x\n", hr);
125
126     hr = IAudioClient_GetMixFormat(ac, &pwfx);
127     ok(hr == S_OK, "Valid GetMixFormat returns %08x\n", hr);
128
129     trace("Tag: %04x\n", pwfx->wFormatTag);
130     trace("bits: %u\n", pwfx->wBitsPerSample);
131     trace("chan: %u\n", pwfx->nChannels);
132     trace("rate: %u\n", pwfx->nSamplesPerSec);
133     trace("align: %u\n", pwfx->nBlockAlign);
134     trace("extra: %u\n", pwfx->cbSize);
135     ok(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE, "wFormatTag is %x\n", pwfx->wFormatTag);
136     if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
137     {
138         WAVEFORMATEXTENSIBLE *pwfxe = (void*)pwfx;
139         trace("Res: %u\n", pwfxe->Samples.wReserved);
140         trace("Mask: %x\n", pwfxe->dwChannelMask);
141         trace("Alg: %s\n",
142               IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)?"PCM":
143               (IsEqualGUID(&pwfxe->SubFormat,
144                            &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)?"FLOAT":"Other"));
145     }
146
147     if (hr == S_OK)
148     {
149         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, &pwfx2);
150         ok(hr == S_OK, "Valid IsFormatSupported(Shared) call returns %08x\n", hr);
151         ok(pwfx2 == NULL, "pwfx2 is non-null\n");
152         CoTaskMemFree(pwfx2);
153
154         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, NULL, NULL);
155         ok(hr == E_POINTER, "IsFormatSupported(NULL) call returns %08x\n", hr);
156
157         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_SHARED, pwfx, NULL);
158         ok(hr == E_POINTER, "IsFormatSupported(Shared,NULL) call returns %08x\n", hr);
159
160         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, NULL);
161         ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
162
163         hr = IAudioClient_IsFormatSupported(ac, AUDCLNT_SHAREMODE_EXCLUSIVE, pwfx, &pwfx2);
164         ok(hr == S_OK || hr == AUDCLNT_E_UNSUPPORTED_FORMAT, "IsFormatSupported(Exclusive) call returns %08x\n", hr);
165         ok(pwfx2 == NULL, "pwfx2 non-null on exclusive IsFormatSupported\n");
166
167         hr = IAudioClient_IsFormatSupported(ac, 0xffffffff, pwfx, NULL);
168         ok(hr == E_INVALIDARG, "IsFormatSupported(0xffffffff) call returns %08x\n", hr);
169     }
170
171     test_uninitialized(ac);
172
173     hr = IAudioClient_Initialize(ac, 3, 0, 5000000, 0, pwfx, NULL);
174     ok(hr == AUDCLNT_E_NOT_INITIALIZED, "Initialize with invalid sharemode returns %08x\n", hr);
175
176     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0xffffffff, 5000000, 0, pwfx, NULL);
177     ok(hr == E_INVALIDARG, "Initialize with invalid flags returns %08x\n", hr);
178
179     /* It seems that if length > 2s or periodicity != 0 the length is ignored and call succeeds
180      * Since we can only initialize succesfully once skip those tests
181      */
182     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, NULL, NULL);
183     ok(hr == E_POINTER, "Initialize with null format returns %08x\n", hr);
184
185     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
186     ok(hr == S_OK, "Valid Initialize returns %08x\n", hr);
187
188     if (hr != S_OK)
189     {
190         skip("Cannot initialize %08x, remainder of tests is useless\n", hr);
191         CoTaskMemFree(pwfx);
192         return;
193     }
194
195     hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL);
196     ok(hr == AUDCLNT_E_ALREADY_INITIALIZED, "Calling Initialize twice returns %08x\n", hr);
197
198     hr = IAudioClient_SetEventHandle(ac, NULL);
199     ok(hr == E_INVALIDARG, "SetEventHandle(NULL) returns %08x\n", hr);
200
201     hr = IAudioClient_SetEventHandle(ac, handle);
202     ok(hr == AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED ||
203        hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME)
204        , "SetEventHandle returns %08x\n", hr);
205
206     CloseHandle(handle);
207     CoTaskMemFree(pwfx);
208 }
209
210 START_TEST(render)
211 {
212     HRESULT hr;
213     IMMDeviceEnumerator *mme = NULL;
214     IMMDevice *dev = NULL;
215     IAudioClient *ac = NULL;
216
217     CoInitializeEx(NULL, COINIT_MULTITHREADED);
218     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
219     if (FAILED(hr))
220     {
221         skip("mmdevapi not available: 0x%08x\n", hr);
222         goto cleanup;
223     }
224
225     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
226     ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
227     if (hr != S_OK || !dev)
228     {
229         if (hr == E_NOTFOUND)
230             skip("No sound card available\n");
231         else
232             skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
233         goto cleanup;
234     }
235
236     hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, (void**)&ac);
237     ok(hr == S_OK, "Activation failed with %08x\n", hr);
238     if (ac)
239     {
240         test_audioclient(ac);
241         IAudioClient_Release(ac);
242     }
243     IMMDevice_Release(dev);
244
245 cleanup:
246     if (mme)
247         IMMDeviceEnumerator_Release(mme);
248     CoUninitialize();
249 }