mcicda: Exclude unused headers.
[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     DSBUFFERDESC bufdesc;
399     DSCAPS dscaps;
400     WAVEFORMATEX wfx;
401     int ref;
402
403     /* Create the DirectSound object */
404     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
405     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
406        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
407     if (rc!=DS_OK)
408         return rc;
409
410     /* Get the device capabilities */
411     ZeroMemory(&dscaps, sizeof(dscaps));
412     dscaps.dwSize=sizeof(dscaps);
413     rc=IDirectSound8_GetCaps(dso,&dscaps);
414     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
415     if (rc!=DS_OK)
416         goto EXIT;
417
418     /* DSOUND: Error: Invalid buffer description pointer */
419     rc=IDirectSound8_CreateSoundBuffer(dso,0,0,NULL);
420     ok(rc==DSERR_INVALIDPARAM,
421        "IDirectSound8_CreateSoundBuffer should have returned "
422        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
423
424     /* DSOUND: Error: Invalid buffer description pointer */
425     rc=IDirectSound8_CreateSoundBuffer(dso,0,&primary,NULL);
426     ok(rc==DSERR_INVALIDPARAM && primary==0,
427        "IDirectSound8_CreateSoundBuffer() should have returned "
428        "DSERR_INVALIDPARAM, returned: rc=%s,dsbo=%p\n",
429        DXGetErrorString8(rc),primary);
430
431     /* DSOUND: Error: Invalid buffer description pointer */
432     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,0,NULL);
433     ok(rc==DSERR_INVALIDPARAM && primary==0,
434        "IDirectSound8_CreateSoundBuffer() should have failed: rc=%s,"
435        "dsbo=%p\n",DXGetErrorString8(rc),primary);
436
437     ZeroMemory(&bufdesc, sizeof(bufdesc));
438
439     /* DSOUND: Error: Invalid size */
440     /* DSOUND: Error: Invalid buffer description */
441     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
442     ok(rc==DSERR_INVALIDPARAM && primary==0,
443        "IDirectSound8_CreateSoundBuffer() should have failed: rc=%s,"
444        "primary=%p\n",DXGetErrorString8(rc),primary);
445
446     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
447     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
448     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
449     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
450        DXGetErrorString8(rc));
451     if (rc!=DS_OK)
452         goto EXIT;
453
454     /* Testing the primary buffer */
455     primary=NULL;
456     ZeroMemory(&bufdesc, sizeof(bufdesc));
457     bufdesc.dwSize=sizeof(bufdesc);
458     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
459     bufdesc.lpwfxFormat = &wfx;
460     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
461     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
462     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_CreateSoundBuffer() should have "
463        "returned DSERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(rc));
464     if (rc==DS_OK && primary!=NULL)
465         IDirectSoundBuffer_Release(primary);
466
467     primary=NULL;
468     ZeroMemory(&bufdesc, sizeof(bufdesc));
469     bufdesc.dwSize=sizeof(bufdesc);
470     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
471     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
472     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
473        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: "
474        "%s\n",DXGetErrorString8(rc));
475     if (rc==DSERR_CONTROLUNAVAIL)
476         trace("  No Primary\n");
477     else if (rc==DS_OK && primary!=NULL) {
478         LONG vol;
479
480         /* Try to create a second primary buffer */
481         /* DSOUND: Error: The primary buffer already exists.
482          * Any changes made to the buffer description will be ignored. */
483         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
484         ok(rc==DS_OK && second==primary,
485            "IDirectSound8_CreateSoundBuffer() should have returned original "
486            "primary buffer: %s\n",DXGetErrorString8(rc));
487         ref=IDirectSoundBuffer_Release(second);
488         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
489            "should have 1\n",ref);
490
491         /* Try to duplicate a primary buffer */
492         /* DSOUND: Error: Can't duplicate primary buffers */
493         rc=IDirectSound8_DuplicateSoundBuffer(dso,primary,&third);
494         /* rc=0x88780032 */
495         ok(rc!=DS_OK,"IDirectSound8_DuplicateSoundBuffer() primary buffer "
496            "should have failed %s\n",DXGetErrorString8(rc));
497
498         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
499         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
500            DXGetErrorString8(rc));
501
502         if (winetest_interactive) {
503             trace("Playing a 5 seconds reference tone at the current volume.\n");
504             if (rc==DS_OK)
505                 trace("(the current volume is %d according to DirectSound)\n",
506                       vol);
507             trace("All subsequent tones should be identical to this one.\n");
508             trace("Listen for stutter, changes in pitch, volume, etc.\n");
509         }
510         test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
511                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0);
512
513         ref=IDirectSoundBuffer_Release(primary);
514         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
515            "should have 0\n",ref);
516     }
517
518     /* Set the CooperativeLevel back to normal */
519     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
520     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
521     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
522        DXGetErrorString8(rc));
523
524 EXIT:
525     ref=IDirectSound8_Release(dso);
526     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
527     if (ref!=0)
528         return DSERR_GENERIC;
529
530     return rc;
531 }
532
533 /*
534  * Test the primary buffer at different formats while keeping the
535  * secondary buffer at a constant format.
536  */
537 static HRESULT test_primary_secondary8(LPGUID lpGuid)
538 {
539     HRESULT rc;
540     LPDIRECTSOUND8 dso=NULL;
541     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
542     DSBUFFERDESC bufdesc;
543     DSCAPS dscaps;
544     WAVEFORMATEX wfx, wfx2;
545     int ref;
546     unsigned int f;
547
548     /* Create the DirectSound object */
549     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
550     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
551        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
552     if (rc!=DS_OK)
553         return rc;
554
555     /* Get the device capabilities */
556     ZeroMemory(&dscaps, sizeof(dscaps));
557     dscaps.dwSize=sizeof(dscaps);
558     rc=IDirectSound8_GetCaps(dso,&dscaps);
559     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
560     if (rc!=DS_OK)
561         goto EXIT;
562
563     /* We must call SetCooperativeLevel before creating primary buffer */
564     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
565     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
566     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
567        DXGetErrorString8(rc));
568     if (rc!=DS_OK)
569         goto EXIT;
570
571     ZeroMemory(&bufdesc, sizeof(bufdesc));
572     bufdesc.dwSize=sizeof(bufdesc);
573     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
574     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
575     ok(rc==DS_OK && primary!=NULL,
576        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
577        "%s\n",DXGetErrorString8(rc));
578
579     if (rc==DS_OK && primary!=NULL) {
580         for (f=0;f<NB_FORMATS;f++) {
581             /* We must call SetCooperativeLevel to be allowed to call
582              * SetFormat */
583             /* DSOUND: Setting DirectSound cooperative level to
584              * DSSCL_PRIORITY */
585             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
586             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
587                DXGetErrorString8(rc));
588             if (rc!=DS_OK)
589                 goto EXIT;
590
591             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
592                         formats[f][2]);
593             wfx2=wfx;
594             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
595             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
596                format_string(&wfx), DXGetErrorString8(rc));
597
598             /* There is no guarantee that SetFormat will actually change the
599              * format to what we asked for. It depends on what the soundcard
600              * supports. So we must re-query the format.
601              */
602             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
603             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
604                DXGetErrorString8(rc));
605             if (rc==DS_OK &&
606                 (wfx.wFormatTag!=wfx2.wFormatTag ||
607                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
608                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
609                  wfx.nChannels!=wfx2.nChannels)) {
610                 trace("Requested primary format tag=0x%04x %dx%dx%d "
611                       "avg.B/s=%d align=%d\n",
612                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
613                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
614                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
615                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
616                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
617             }
618
619             /* Set the CooperativeLevel back to normal */
620             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
621             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
622             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
623                DXGetErrorString8(rc));
624
625             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
626
627             secondary=NULL;
628             ZeroMemory(&bufdesc, sizeof(bufdesc));
629             bufdesc.dwSize=sizeof(bufdesc);
630             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
631             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
632                                         wfx.nBlockAlign);
633             bufdesc.lpwfxFormat=&wfx2;
634             if (winetest_interactive) {
635                 trace("  Testing a primary buffer at %dx%dx%d with a "
636                       "secondary buffer at %dx%dx%d\n",
637                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
638                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
639             }
640             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
641             ok(rc==DS_OK && secondary!=NULL,
642                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
643                "buffer %s\n",DXGetErrorString8(rc));
644
645             if (rc==DS_OK && secondary!=NULL) {
646                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
647                              winetest_interactive,1.0,0,NULL,0,0);
648
649                 ref=IDirectSoundBuffer_Release(secondary);
650                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
651                    "should have 0\n",ref);
652             }
653         }
654
655         ref=IDirectSoundBuffer_Release(primary);
656         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
657            "should have 0\n",ref);
658     }
659
660     /* Set the CooperativeLevel back to normal */
661     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
662     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
663     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
664        DXGetErrorString8(rc));
665
666 EXIT:
667     ref=IDirectSound8_Release(dso);
668     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
669     if (ref!=0)
670         return DSERR_GENERIC;
671
672     return rc;
673 }
674
675 static HRESULT test_secondary8(LPGUID lpGuid)
676 {
677     HRESULT rc;
678     LPDIRECTSOUND8 dso=NULL;
679     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
680     DSBUFFERDESC bufdesc;
681     DSCAPS dscaps;
682     WAVEFORMATEX wfx, wfx1;
683     DWORD f;
684     int ref;
685
686     /* Create the DirectSound object */
687     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
688     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
689        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
690     if (rc!=DS_OK)
691         return rc;
692
693     /* Get the device capabilities */
694     ZeroMemory(&dscaps, sizeof(dscaps));
695     dscaps.dwSize=sizeof(dscaps);
696     rc=IDirectSound8_GetCaps(dso,&dscaps);
697     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
698     if (rc!=DS_OK)
699         goto EXIT;
700
701     /* We must call SetCooperativeLevel before creating primary buffer */
702     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
703     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
704     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
705        DXGetErrorString8(rc));
706     if (rc!=DS_OK)
707         goto EXIT;
708
709     ZeroMemory(&bufdesc, sizeof(bufdesc));
710     bufdesc.dwSize=sizeof(bufdesc);
711     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
712     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
713     ok(rc==DS_OK && primary!=NULL,
714        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
715        "%s\n",DXGetErrorString8(rc));
716
717     if (rc==DS_OK && primary!=NULL) {
718         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
719         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
720            DXGetErrorString8(rc));
721         if (rc!=DS_OK)
722             goto EXIT1;
723
724         for (f=0;f<NB_FORMATS;f++) {
725             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
726                         formats[f][2]);
727             secondary=NULL;
728             ZeroMemory(&bufdesc, sizeof(bufdesc));
729             bufdesc.dwSize=sizeof(bufdesc);
730             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
731             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
732                                         wfx.nBlockAlign);
733             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
734             ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_CreateSoundBuffer() "
735                "should have returned DSERR_INVALIDPARAM, returned: %s\n",
736                DXGetErrorString8(rc));
737             if (rc==DS_OK && secondary!=NULL)
738                 IDirectSoundBuffer_Release(secondary);
739
740             secondary=NULL;
741             ZeroMemory(&bufdesc, sizeof(bufdesc));
742             bufdesc.dwSize=sizeof(bufdesc);
743             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
744             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
745                                         wfx.nBlockAlign);
746             bufdesc.lpwfxFormat=&wfx;
747             if (winetest_interactive) {
748                 trace("  Testing a secondary buffer at %dx%dx%d "
749                       "with a primary buffer at %dx%dx%d\n",
750                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
751                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
752             }
753             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
754             ok(rc==DS_OK && secondary!=NULL,
755                "IDirectSound8_CreateSoundBuffer() failed to create a secondary "
756                "buffer: %s\n",DXGetErrorString8(rc));
757
758             if (rc==DS_OK && secondary!=NULL) {
759                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
760                              winetest_interactive,1.0,0,NULL,0,0);
761
762                 ref=IDirectSoundBuffer_Release(secondary);
763                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
764                    "should have 0\n",ref);
765             }
766         }
767 EXIT1:
768         ref=IDirectSoundBuffer_Release(primary);
769         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
770            "should have 0\n",ref);
771     }
772
773     /* Set the CooperativeLevel back to normal */
774     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
775     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
776     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
777        DXGetErrorString8(rc));
778
779 EXIT:
780     ref=IDirectSound8_Release(dso);
781     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
782     if (ref!=0)
783         return DSERR_GENERIC;
784
785     return rc;
786 }
787
788 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
789                                    LPCSTR lpcstrModule, LPVOID lpContext)
790 {
791     HRESULT rc;
792     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
793     rc = test_dsound8(lpGuid);
794     if (rc == DSERR_NODRIVER)
795         trace("  No Driver\n");
796     else if (rc == DSERR_ALLOCATED)
797         trace("  Already In Use\n");
798     else if (rc == E_FAIL)
799         trace("  No Device\n");
800     else {
801         test_primary8(lpGuid);
802         test_primary_secondary8(lpGuid);
803         test_secondary8(lpGuid);
804     }
805
806     return 1;
807 }
808
809 static void dsound8_tests(void)
810 {
811     HRESULT rc;
812     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
813     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
814 }
815
816 const char * get_file_version(const char * file_name)
817 {
818     static char version[32];
819     static char backslash[] = "\\";
820     DWORD size;
821     DWORD handle;
822
823     size = GetFileVersionInfoSizeA("dsound.dll", &handle);
824     if (size) {
825         char * data = HeapAlloc(GetProcessHeap(), 0, size);
826         if (data) {
827             if (GetFileVersionInfoA("dsound.dll", handle, size, data)) {
828                 VS_FIXEDFILEINFO *pFixedVersionInfo;
829                 UINT len;
830                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
831                     sprintf(version, "%d.%d.%d.%d",
832                             pFixedVersionInfo->dwFileVersionMS >> 16,
833                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
834                             pFixedVersionInfo->dwFileVersionLS >> 16,
835                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
836                 } else
837                     sprintf(version, "not available");
838             } else
839                 sprintf(version, "failed");
840
841             HeapFree(GetProcessHeap(), 0, data);
842         } else
843             sprintf(version, "failed");
844     } else
845         sprintf(version, "not available");
846
847     return version;
848 }
849
850 START_TEST(dsound8)
851 {
852     HMODULE hDsound;
853
854     CoInitialize(NULL);
855
856     hDsound = LoadLibrary("dsound.dll");
857     if (hDsound)
858     {
859         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
860
861         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
862             "DirectSoundEnumerateA");
863         pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
864             "DirectSoundCreate8");
865         if (pDirectSoundCreate8)
866         {
867             IDirectSound8_tests();
868             dsound8_tests();
869         }
870         else
871             skip("dsound8 test skipped\n");
872
873         FreeLibrary(hDsound);
874     }
875     else
876         skip("dsound.dll not found!\n");
877
878     CoUninitialize();
879 }