ntdll: Fix compilation on systems that don't support nameless unions.
[wine] / dlls / dsound / tests / dsound8.c
1 /*
2  * Tests basic sound playback in DirectSound.
3  * In particular we test each standard Windows sound format to make sure
4  * we handle the sound card/driver quirks correctly.
5  *
6  * Part of this test involves playing test tones. But this only makes
7  * sense if someone is going to carefully listen to it, and would only
8  * bother everyone else.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (c) 2002-2004 Francois Gouget
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27
28 #include <windows.h>
29 #include <stdio.h>
30
31 #include "wine/test.h"
32 #include "dsound.h"
33 #include "dxerr8.h"
34 #include "dsconf.h"
35
36 #include "dsound_test.h"
37
38 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
39 static HRESULT (WINAPI *pDirectSoundCreate8)(LPCGUID,LPDIRECTSOUND8*,LPUNKNOWN)=NULL;
40
41 int align(int length, int align)
42 {
43     return (length / align) * align;
44 }
45
46 static void IDirectSound8_test(LPDIRECTSOUND8 dso, BOOL initialized,
47                                LPCGUID lpGuid)
48 {
49     HRESULT rc;
50     DSCAPS dscaps;
51     int ref;
52     IUnknown * unknown;
53     IDirectSound * ds;
54     IDirectSound8 * ds8;
55     DWORD speaker_config, new_speaker_config;
56     DWORD certified;
57
58     /* Try to Query for objects */
59     rc=IDirectSound8_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
60     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IUnknown) failed: %s\n",
61        DXGetErrorString8(rc));
62     if (rc==DS_OK)
63         IDirectSound8_Release(unknown);
64
65     rc=IDirectSound8_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
66     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IDirectSound) failed: %s\n",
67        DXGetErrorString8(rc));
68     if (rc==DS_OK)
69         IDirectSound_Release(ds);
70
71     rc=IDirectSound8_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
72     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IDirectSound8) "
73        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
74        DXGetErrorString8(rc));
75     if (rc==DS_OK)
76         IDirectSound8_Release(ds8);
77
78     if (initialized == FALSE) {
79         /* try uninitialized object */
80         rc=IDirectSound8_GetCaps(dso,0);
81         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetCaps(NULL) "
82            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
83            DXGetErrorString8(rc));
84
85         rc=IDirectSound8_GetCaps(dso,&dscaps);
86         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetCaps() "
87            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
88            DXGetErrorString8(rc));
89
90         rc=IDirectSound8_Compact(dso);
91         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_Compact() "
92            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
93            DXGetErrorString8(rc));
94
95         rc=IDirectSound8_GetSpeakerConfig(dso,&speaker_config);
96         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetSpeakerConfig() "
97            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
98            DXGetErrorString8(rc));
99
100         rc=IDirectSound8_VerifyCertification(dso, &certified);
101         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_VerifyCertification() "
102            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
103            DXGetErrorString8(rc));
104
105         rc=IDirectSound8_Initialize(dso,lpGuid);
106         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
107            "IDirectSound8_Initialize() failed: %s\n",DXGetErrorString8(rc));
108         if (rc==DSERR_NODRIVER) {
109             trace("  No Driver\n");
110             goto EXIT;
111         } else if (rc==E_FAIL) {
112             trace("  No Device\n");
113             goto EXIT;
114         } else if (rc==DSERR_ALLOCATED) {
115             trace("  Already In Use\n");
116             goto EXIT;
117         }
118     }
119
120     rc=IDirectSound8_Initialize(dso,lpGuid);
121     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound8_Initialize() "
122        "should have returned DSERR_ALREADYINITIALIZED: %s\n",
123        DXGetErrorString8(rc));
124
125     /* DSOUND: Error: Invalid caps buffer */
126     rc=IDirectSound8_GetCaps(dso,0);
127     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetCaps() "
128        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
129        DXGetErrorString8(rc));
130
131     ZeroMemory(&dscaps, sizeof(dscaps));
132
133     /* DSOUND: Error: Invalid caps buffer */
134     rc=IDirectSound8_GetCaps(dso,&dscaps);
135     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetCaps() "
136        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
137        DXGetErrorString8(rc));
138
139     dscaps.dwSize=sizeof(dscaps);
140
141     /* DSOUND: Running on a certified driver */
142     rc=IDirectSound8_GetCaps(dso,&dscaps);
143     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
144
145     rc=IDirectSound8_Compact(dso);
146     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound8_Compact() failed: %s\n",
147        DXGetErrorString8(rc));
148
149     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
150     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
151        DXGetErrorString8(rc));
152
153     rc=IDirectSound8_Compact(dso);
154     ok(rc==DS_OK,"IDirectSound8_Compact() failed: %s\n",DXGetErrorString8(rc));
155
156     rc=IDirectSound8_GetSpeakerConfig(dso,0);
157     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetSpeakerConfig(NULL) "
158        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
159        DXGetErrorString8(rc));
160
161     rc=IDirectSound8_GetSpeakerConfig(dso,&speaker_config);
162     ok(rc==DS_OK,"IDirectSound8_GetSpeakerConfig() failed: %s\n",
163        DXGetErrorString8(rc));
164
165     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
166                                         DSSPEAKER_GEOMETRY_WIDE);
167     rc=IDirectSound8_SetSpeakerConfig(dso,speaker_config);
168     ok(rc==DS_OK,"IDirectSound8_SetSpeakerConfig() failed: %s\n",
169        DXGetErrorString8(rc));
170     if (rc==DS_OK) {
171         rc=IDirectSound8_GetSpeakerConfig(dso,&new_speaker_config);
172         ok(rc==DS_OK,"IDirectSound8_GetSpeakerConfig() failed: %s\n",
173            DXGetErrorString8(rc));
174         if (rc==DS_OK && speaker_config!=new_speaker_config)
175                trace("IDirectSound8_GetSpeakerConfig() failed to set speaker "
176                "config: expected 0x%08x, got 0x%08x\n",
177                speaker_config,new_speaker_config);
178     }
179
180     rc=IDirectSound8_VerifyCertification(dso, &certified);
181     ok(rc==DS_OK||rc==E_NOTIMPL,"IDirectSound8_VerifyCertification() failed: %s\n",
182        DXGetErrorString8(rc));
183
184 EXIT:
185     ref=IDirectSound8_Release(dso);
186     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
187 }
188
189 static void IDirectSound8_tests(void)
190 {
191     HRESULT rc;
192     LPDIRECTSOUND8 dso=NULL;
193     LPCLASSFACTORY cf=NULL;
194
195     trace("Testing IDirectSound8\n");
196
197     rc=CoGetClassObject(&CLSID_DirectSound8, CLSCTX_INPROC_SERVER, NULL,
198                         &IID_IClassFactory, (void**)&cf);
199     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound8, IID_IClassFactory) "
200        "failed: %s\n", DXGetErrorString8(rc));
201
202     rc=CoGetClassObject(&CLSID_DirectSound8, CLSCTX_INPROC_SERVER, NULL,
203                         &IID_IUnknown, (void**)&cf);
204     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound8, IID_IUnknown) "
205        "failed: %s\n", DXGetErrorString8(rc));
206
207     /* try the COM class factory method of creation with no device specified */
208     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
209                         &IID_IDirectSound8, (void**)&dso);
210     ok(rc==S_OK||rc==REGDB_E_CLASSNOTREG,"CoCreateInstance() failed: %s\n",
211        DXGetErrorString8(rc));
212     if (rc==REGDB_E_CLASSNOTREG) {
213         trace("  Class Not Registered\n");
214         return;
215     }
216     if (dso)
217         IDirectSound8_test(dso, FALSE, NULL);
218
219     /* try the COM class factory method of creation with default playback
220      *  device specified */
221     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
222                         &IID_IDirectSound8, (void**)&dso);
223     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound8) failed: %s\n",
224        DXGetErrorString8(rc));
225     if (dso)
226         IDirectSound8_test(dso, FALSE, &DSDEVID_DefaultPlayback);
227
228     /* try the COM class factory method of creation with default voice
229      * playback device specified */
230     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
231                         &IID_IDirectSound8, (void**)&dso);
232     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound8) failed: %s\n",
233        DXGetErrorString8(rc));
234     if (dso)
235         IDirectSound8_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
236
237     /* try the COM class factory method of creation with a bad
238      * IID specified */
239     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
240                         &CLSID_DirectSoundPrivate, (void**)&dso);
241     ok(rc==E_NOINTERFACE,
242        "CoCreateInstance(CLSID_DirectSound8,CLSID_DirectSoundPrivate) "
243        "should have failed: %s\n",DXGetErrorString8(rc));
244
245     /* try the COM class factory method of creation with a bad
246      * GUID and IID specified */
247     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
248                         &IID_IDirectSound8, (void**)&dso);
249     ok(rc==REGDB_E_CLASSNOTREG,
250        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound8) "
251        "should have failed: %s\n",DXGetErrorString8(rc));
252
253     /* try with no device specified */
254     rc=pDirectSoundCreate8(NULL,&dso,NULL);
255     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
256        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
257     if (rc==DS_OK && dso)
258         IDirectSound8_test(dso, TRUE, NULL);
259
260     /* try with default playback device specified */
261     rc=pDirectSoundCreate8(&DSDEVID_DefaultPlayback,&dso,NULL);
262     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
263        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
264     if (rc==DS_OK && dso)
265         IDirectSound8_test(dso, TRUE, NULL);
266
267     /* try with default voice playback device specified */
268     rc=pDirectSoundCreate8(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
269     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
270        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
271     if (rc==DS_OK && dso)
272         IDirectSound8_test(dso, TRUE, NULL);
273
274     /* try with a bad device specified */
275     rc=pDirectSoundCreate8(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
276     ok(rc==DSERR_NODRIVER,"DirectSoundCreate8(DSDEVID_DefaultVoiceCapture) "
277        "should have failed: %s\n",DXGetErrorString8(rc));
278 }
279
280 static HRESULT test_dsound8(LPGUID lpGuid)
281 {
282     HRESULT rc;
283     LPDIRECTSOUND8 dso=NULL;
284     int ref;
285
286     /* DSOUND: Error: Invalid interface buffer */
287     rc=pDirectSoundCreate8(lpGuid,0,NULL);
288     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate8() should have returned "
289        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
290
291     /* Create the DirectSound8 object */
292     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
293     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
294        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
295     if (rc!=DS_OK)
296         return rc;
297
298     /* Try the enumerated device */
299     IDirectSound8_test(dso, TRUE, lpGuid);
300
301     /* Try the COM class factory method of creation with enumerated device */
302     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
303                         &IID_IDirectSound8, (void**)&dso);
304     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
305        DXGetErrorString8(rc));
306     if (dso)
307         IDirectSound8_test(dso, FALSE, lpGuid);
308
309     /* Create a DirectSound8 object */
310     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
311     ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
312     if (rc==DS_OK) {
313         LPDIRECTSOUND8 dso1=NULL;
314
315         /* Create a second DirectSound8 object */
316         rc=pDirectSoundCreate8(lpGuid,&dso1,NULL);
317         ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
318         if (rc==DS_OK) {
319             /* Release the second DirectSound8 object */
320             ref=IDirectSound8_Release(dso1);
321             ok(ref==0,"IDirectSound8_Release() has %d references, "
322                "should have 0\n",ref);
323             ok(dso!=dso1,"DirectSound8 objects should be unique: "
324                "dso=%p,dso1=%p\n",dso,dso1);
325         }
326
327         /* Release the first DirectSound8 object */
328         ref=IDirectSound8_Release(dso);
329         ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",
330            ref);
331         if (ref!=0)
332             return DSERR_GENERIC;
333     } else
334         return rc;
335
336     /* Create a DirectSound8 object */
337     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
338     ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
339     if (rc==DS_OK) {
340         LPDIRECTSOUNDBUFFER secondary;
341         DSBUFFERDESC bufdesc;
342         WAVEFORMATEX wfx;
343
344         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
345         ZeroMemory(&bufdesc, sizeof(bufdesc));
346         bufdesc.dwSize=sizeof(bufdesc);
347         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
348         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
349                                     wfx.nBlockAlign);
350         bufdesc.lpwfxFormat=&wfx;
351         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
352         ok(rc==DS_OK && secondary!=NULL,
353            "IDirectSound8_CreateSoundBuffer() failed to create a secondary "
354            "buffer: %s\n",DXGetErrorString8(rc));
355         if (rc==DS_OK && secondary!=NULL) {
356             LPDIRECTSOUND3DBUFFER buffer3d;
357             LPDIRECTSOUNDBUFFER8 buffer8;
358             rc=IDirectSound8_QueryInterface(secondary,
359                                             &IID_IDirectSound3DBuffer,
360                                             (void **)&buffer3d);
361             ok(rc==DS_OK && buffer3d!=NULL,
362                "IDirectSound8_QueryInterface() failed: %s\n",
363                DXGetErrorString8(rc));
364             if (rc==DS_OK && buffer3d!=NULL) {
365                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
366                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
367                    "should have 2\n",ref);
368             }
369             rc=IDirectSound8_QueryInterface(secondary,
370                                             &IID_IDirectSoundBuffer8,
371                                             (void **)&buffer8);
372             if (rc==DS_OK && buffer8!=NULL) {
373                 ref=IDirectSoundBuffer8_AddRef(buffer8);
374                 ok(ref==3,"IDirectSoundBuffer8_AddRef() has %d references, "
375                    "should have 3\n",ref);
376             }
377             ref=IDirectSoundBuffer_AddRef(secondary);
378             ok(ref==4,"IDirectSoundBuffer_AddRef() has %d references, "
379                "should have 4\n",ref);
380         }
381         /* release with buffer */
382         ref=IDirectSound8_Release(dso);
383         ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",
384            ref);
385         if (ref!=0)
386             return DSERR_GENERIC;
387     } else
388         return rc;
389
390     return DS_OK;
391 }
392
393 static HRESULT test_primary8(LPGUID lpGuid)
394 {
395     HRESULT rc;
396     LPDIRECTSOUND8 dso=NULL;
397     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
398     LPDIRECTSOUNDBUFFER8 pb8 = NULL;
399     DSBUFFERDESC bufdesc;
400     DSCAPS dscaps;
401     WAVEFORMATEX wfx;
402     int ref;
403
404     /* Create the DirectSound object */
405     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
406     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
407        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
408     if (rc!=DS_OK)
409         return rc;
410
411     /* Get the device capabilities */
412     ZeroMemory(&dscaps, sizeof(dscaps));
413     dscaps.dwSize=sizeof(dscaps);
414     rc=IDirectSound8_GetCaps(dso,&dscaps);
415     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
416     if (rc!=DS_OK)
417         goto EXIT;
418
419     /* DSOUND: Error: Invalid buffer description pointer */
420     rc=IDirectSound8_CreateSoundBuffer(dso,0,0,NULL);
421     ok(rc==DSERR_INVALIDPARAM,
422        "IDirectSound8_CreateSoundBuffer should have returned "
423        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
424
425     /* DSOUND: Error: Invalid buffer description pointer */
426     rc=IDirectSound8_CreateSoundBuffer(dso,0,&primary,NULL);
427     ok(rc==DSERR_INVALIDPARAM && primary==0,
428        "IDirectSound8_CreateSoundBuffer() should have returned "
429        "DSERR_INVALIDPARAM, returned: rc=%s,dsbo=%p\n",
430        DXGetErrorString8(rc),primary);
431
432     /* DSOUND: Error: Invalid buffer description pointer */
433     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,0,NULL);
434     ok(rc==DSERR_INVALIDPARAM && primary==0,
435        "IDirectSound8_CreateSoundBuffer() should have failed: rc=%s,"
436        "dsbo=%p\n",DXGetErrorString8(rc),primary);
437
438     ZeroMemory(&bufdesc, sizeof(bufdesc));
439
440     /* DSOUND: Error: Invalid size */
441     /* DSOUND: Error: Invalid buffer description */
442     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
443     ok(rc==DSERR_INVALIDPARAM && primary==0,
444        "IDirectSound8_CreateSoundBuffer() should have failed: rc=%s,"
445        "primary=%p\n",DXGetErrorString8(rc),primary);
446
447     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
448     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
449     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
450     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
451        DXGetErrorString8(rc));
452     if (rc!=DS_OK)
453         goto EXIT;
454
455     /* Testing the primary buffer */
456     primary=NULL;
457     ZeroMemory(&bufdesc, sizeof(bufdesc));
458     bufdesc.dwSize=sizeof(bufdesc);
459     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
460     bufdesc.lpwfxFormat = &wfx;
461     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
462     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
463     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_CreateSoundBuffer() should have "
464        "returned DSERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(rc));
465     if (rc==DS_OK && primary!=NULL)
466         IDirectSoundBuffer_Release(primary);
467
468     primary=NULL;
469     ZeroMemory(&bufdesc, sizeof(bufdesc));
470     bufdesc.dwSize=sizeof(bufdesc);
471     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
472     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
473     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
474        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: "
475        "%s\n",DXGetErrorString8(rc));
476     if (rc==DSERR_CONTROLUNAVAIL)
477         trace("  No Primary\n");
478     else if (rc==DS_OK && primary!=NULL) {
479         LONG vol;
480
481         /* Try to create a second primary buffer */
482         /* DSOUND: Error: The primary buffer already exists.
483          * Any changes made to the buffer description will be ignored. */
484         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
485         ok(rc==DS_OK && second==primary,
486            "IDirectSound8_CreateSoundBuffer() should have returned original "
487            "primary buffer: %s\n",DXGetErrorString8(rc));
488         ref=IDirectSoundBuffer_Release(second);
489         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
490            "should have 1\n",ref);
491
492         /* Try to duplicate a primary buffer */
493         /* DSOUND: Error: Can't duplicate primary buffers */
494         rc=IDirectSound8_DuplicateSoundBuffer(dso,primary,&third);
495         /* rc=0x88780032 */
496         ok(rc!=DS_OK,"IDirectSound8_DuplicateSoundBuffer() primary buffer "
497            "should have failed %s\n",DXGetErrorString8(rc));
498
499         /* Primary buffers don't have an IDirectSoundBuffer8 */
500         rc = IDirectSoundBuffer_QueryInterface(primary, &IID_IDirectSoundBuffer8, (LPVOID*)&pb8);
501         ok(FAILED(rc), "Primary buffer does have an IDirectSoundBuffer8: %s\n", DXGetErrorString8(rc));
502
503         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
504         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
505            DXGetErrorString8(rc));
506
507         if (winetest_interactive) {
508             trace("Playing a 5 seconds reference tone at the current volume.\n");
509             if (rc==DS_OK)
510                 trace("(the current volume is %d according to DirectSound)\n",
511                       vol);
512             trace("All subsequent tones should be identical to this one.\n");
513             trace("Listen for stutter, changes in pitch, volume, etc.\n");
514         }
515         test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
516                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0);
517
518         ref=IDirectSoundBuffer_Release(primary);
519         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
520            "should have 0\n",ref);
521     }
522
523     /* Set the CooperativeLevel back to normal */
524     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
525     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
526     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
527        DXGetErrorString8(rc));
528
529 EXIT:
530     ref=IDirectSound8_Release(dso);
531     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
532     if (ref!=0)
533         return DSERR_GENERIC;
534
535     return rc;
536 }
537
538 /*
539  * Test the primary buffer at different formats while keeping the
540  * secondary buffer at a constant format.
541  */
542 static HRESULT test_primary_secondary8(LPGUID lpGuid)
543 {
544     HRESULT rc;
545     LPDIRECTSOUND8 dso=NULL;
546     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
547     DSBUFFERDESC bufdesc;
548     DSCAPS dscaps;
549     WAVEFORMATEX wfx, wfx2;
550     int ref;
551     unsigned int f;
552
553     /* Create the DirectSound object */
554     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
555     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
556        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
557     if (rc!=DS_OK)
558         return rc;
559
560     /* Get the device capabilities */
561     ZeroMemory(&dscaps, sizeof(dscaps));
562     dscaps.dwSize=sizeof(dscaps);
563     rc=IDirectSound8_GetCaps(dso,&dscaps);
564     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
565     if (rc!=DS_OK)
566         goto EXIT;
567
568     /* We must call SetCooperativeLevel before creating primary buffer */
569     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
570     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
571     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
572        DXGetErrorString8(rc));
573     if (rc!=DS_OK)
574         goto EXIT;
575
576     ZeroMemory(&bufdesc, sizeof(bufdesc));
577     bufdesc.dwSize=sizeof(bufdesc);
578     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
579     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
580     ok(rc==DS_OK && primary!=NULL,
581        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
582        "%s\n",DXGetErrorString8(rc));
583
584     if (rc==DS_OK && primary!=NULL) {
585         for (f=0;f<NB_FORMATS;f++) {
586             /* We must call SetCooperativeLevel to be allowed to call
587              * SetFormat */
588             /* DSOUND: Setting DirectSound cooperative level to
589              * DSSCL_PRIORITY */
590             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
591             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
592                DXGetErrorString8(rc));
593             if (rc!=DS_OK)
594                 goto EXIT;
595
596             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
597                         formats[f][2]);
598             wfx2=wfx;
599             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
600             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
601                format_string(&wfx), DXGetErrorString8(rc));
602
603             /* There is no guarantee that SetFormat will actually change the
604              * format to what we asked for. It depends on what the soundcard
605              * supports. So we must re-query the format.
606              */
607             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
608             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
609                DXGetErrorString8(rc));
610             if (rc==DS_OK &&
611                 (wfx.wFormatTag!=wfx2.wFormatTag ||
612                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
613                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
614                  wfx.nChannels!=wfx2.nChannels)) {
615                 trace("Requested primary format tag=0x%04x %dx%dx%d "
616                       "avg.B/s=%d align=%d\n",
617                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
618                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
619                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
620                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
621                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
622             }
623
624             /* Set the CooperativeLevel back to normal */
625             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
626             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
627             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
628                DXGetErrorString8(rc));
629
630             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
631
632             secondary=NULL;
633             ZeroMemory(&bufdesc, sizeof(bufdesc));
634             bufdesc.dwSize=sizeof(bufdesc);
635             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
636             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
637                                         wfx.nBlockAlign);
638             bufdesc.lpwfxFormat=&wfx2;
639             if (winetest_interactive) {
640                 trace("  Testing a primary buffer at %dx%dx%d with a "
641                       "secondary buffer at %dx%dx%d\n",
642                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
643                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
644             }
645             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
646             ok(rc==DS_OK && secondary!=NULL,
647                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
648                "buffer %s\n",DXGetErrorString8(rc));
649
650             if (rc==DS_OK && secondary!=NULL) {
651                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
652                              winetest_interactive,1.0,0,NULL,0,0);
653
654                 ref=IDirectSoundBuffer_Release(secondary);
655                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
656                    "should have 0\n",ref);
657             }
658         }
659
660         ref=IDirectSoundBuffer_Release(primary);
661         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
662            "should have 0\n",ref);
663     }
664
665     /* Set the CooperativeLevel back to normal */
666     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
667     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
668     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
669        DXGetErrorString8(rc));
670
671 EXIT:
672     ref=IDirectSound8_Release(dso);
673     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
674     if (ref!=0)
675         return DSERR_GENERIC;
676
677     return rc;
678 }
679
680 static HRESULT test_secondary8(LPGUID lpGuid)
681 {
682     HRESULT rc;
683     LPDIRECTSOUND8 dso=NULL;
684     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
685     DSBUFFERDESC bufdesc;
686     DSCAPS dscaps;
687     WAVEFORMATEX wfx, wfx1;
688     DWORD f;
689     int ref;
690
691     /* Create the DirectSound object */
692     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
693     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
694        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
695     if (rc!=DS_OK)
696         return rc;
697
698     /* Get the device capabilities */
699     ZeroMemory(&dscaps, sizeof(dscaps));
700     dscaps.dwSize=sizeof(dscaps);
701     rc=IDirectSound8_GetCaps(dso,&dscaps);
702     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
703     if (rc!=DS_OK)
704         goto EXIT;
705
706     /* We must call SetCooperativeLevel before creating primary buffer */
707     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
708     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
709     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
710        DXGetErrorString8(rc));
711     if (rc!=DS_OK)
712         goto EXIT;
713
714     ZeroMemory(&bufdesc, sizeof(bufdesc));
715     bufdesc.dwSize=sizeof(bufdesc);
716     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
717     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
718     ok(rc==DS_OK && primary!=NULL,
719        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
720        "%s\n",DXGetErrorString8(rc));
721
722     if (rc==DS_OK && primary!=NULL) {
723         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
724         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
725            DXGetErrorString8(rc));
726         if (rc!=DS_OK)
727             goto EXIT1;
728
729         for (f=0;f<NB_FORMATS;f++) {
730             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
731                         formats[f][2]);
732             secondary=NULL;
733             ZeroMemory(&bufdesc, sizeof(bufdesc));
734             bufdesc.dwSize=sizeof(bufdesc);
735             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
736             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
737                                         wfx.nBlockAlign);
738             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
739             ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_CreateSoundBuffer() "
740                "should have returned DSERR_INVALIDPARAM, returned: %s\n",
741                DXGetErrorString8(rc));
742             if (rc==DS_OK && secondary!=NULL)
743                 IDirectSoundBuffer_Release(secondary);
744
745             secondary=NULL;
746             ZeroMemory(&bufdesc, sizeof(bufdesc));
747             bufdesc.dwSize=sizeof(bufdesc);
748             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
749             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
750                                         wfx.nBlockAlign);
751             bufdesc.lpwfxFormat=&wfx;
752             if (winetest_interactive) {
753                 trace("  Testing a secondary buffer at %dx%dx%d "
754                       "with a primary buffer at %dx%dx%d\n",
755                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
756                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
757             }
758             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
759             ok(rc==DS_OK && secondary!=NULL,
760                "IDirectSound8_CreateSoundBuffer() failed to create a secondary "
761                "buffer: %s\n",DXGetErrorString8(rc));
762
763             if (rc==DS_OK && secondary!=NULL) {
764                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
765                              winetest_interactive,1.0,0,NULL,0,0);
766
767                 ref=IDirectSoundBuffer_Release(secondary);
768                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
769                    "should have 0\n",ref);
770             }
771         }
772 EXIT1:
773         ref=IDirectSoundBuffer_Release(primary);
774         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
775            "should have 0\n",ref);
776     }
777
778     /* Set the CooperativeLevel back to normal */
779     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
780     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
781     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
782        DXGetErrorString8(rc));
783
784 EXIT:
785     ref=IDirectSound8_Release(dso);
786     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
787     if (ref!=0)
788         return DSERR_GENERIC;
789
790     return rc;
791 }
792
793 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
794                                    LPCSTR lpcstrModule, LPVOID lpContext)
795 {
796     HRESULT rc;
797     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
798     rc = test_dsound8(lpGuid);
799     if (rc == DSERR_NODRIVER)
800         trace("  No Driver\n");
801     else if (rc == DSERR_ALLOCATED)
802         trace("  Already In Use\n");
803     else if (rc == E_FAIL)
804         trace("  No Device\n");
805     else {
806         test_primary8(lpGuid);
807         test_primary_secondary8(lpGuid);
808         test_secondary8(lpGuid);
809     }
810
811     return 1;
812 }
813
814 static void dsound8_tests(void)
815 {
816     HRESULT rc;
817     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
818     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
819 }
820
821 const char * get_file_version(const char * file_name)
822 {
823     static char version[32];
824     static char backslash[] = "\\";
825     DWORD size;
826     DWORD handle;
827
828     size = GetFileVersionInfoSizeA("dsound.dll", &handle);
829     if (size) {
830         char * data = HeapAlloc(GetProcessHeap(), 0, size);
831         if (data) {
832             if (GetFileVersionInfoA("dsound.dll", handle, size, data)) {
833                 VS_FIXEDFILEINFO *pFixedVersionInfo;
834                 UINT len;
835                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
836                     sprintf(version, "%d.%d.%d.%d",
837                             pFixedVersionInfo->dwFileVersionMS >> 16,
838                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
839                             pFixedVersionInfo->dwFileVersionLS >> 16,
840                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
841                 } else
842                     sprintf(version, "not available");
843             } else
844                 sprintf(version, "failed");
845
846             HeapFree(GetProcessHeap(), 0, data);
847         } else
848             sprintf(version, "failed");
849     } else
850         sprintf(version, "not available");
851
852     return version;
853 }
854
855 START_TEST(dsound8)
856 {
857     HMODULE hDsound;
858
859     CoInitialize(NULL);
860
861     hDsound = LoadLibrary("dsound.dll");
862     if (hDsound)
863     {
864         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
865
866         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
867             "DirectSoundEnumerateA");
868         pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
869             "DirectSoundCreate8");
870         if (pDirectSoundCreate8)
871         {
872             IDirectSound8_tests();
873             dsound8_tests();
874         }
875         else
876             skip("dsound8 test skipped\n");
877
878         FreeLibrary(hDsound);
879     }
880     else
881         skip("dsound.dll not found!\n");
882
883     CoUninitialize();
884 }