dsound/tests: Win64 printf format warning fixes.
[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 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include <windows.h>
31 #include <stdio.h>
32
33 #include "wine/test.h"
34 #include "dsound.h"
35 #include "dxerr8.h"
36 #include "dsconf.h"
37
38 #include "dsound_test.h"
39
40 static HRESULT (WINAPI *pDirectSoundCreate8)(LPCGUID,LPDIRECTSOUND8*,LPUNKNOWN)=NULL;
41
42 int align(int length, int align)
43 {
44     return (length / align) * align;
45 }
46
47 static void IDirectSound8_test(LPDIRECTSOUND8 dso, BOOL initialized,
48                                LPCGUID lpGuid)
49 {
50     HRESULT rc;
51     DSCAPS dscaps;
52     int ref;
53     IUnknown * unknown;
54     IDirectSound * ds;
55     IDirectSound8 * ds8;
56     DWORD speaker_config, new_speaker_config;
57     DWORD certified;
58
59     /* Try to Query for objects */
60     rc=IDirectSound8_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
61     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IUnknown) failed: %s\n",
62        DXGetErrorString8(rc));
63     if (rc==DS_OK)
64         IDirectSound8_Release(unknown);
65
66     rc=IDirectSound8_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
67     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IDirectSound) failed: %s\n",
68        DXGetErrorString8(rc));
69     if (rc==DS_OK)
70         IDirectSound_Release(ds);
71
72     rc=IDirectSound8_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
73     ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IDirectSound8) "
74        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
75        DXGetErrorString8(rc));
76     if (rc==DS_OK)
77         IDirectSound8_Release(ds8);
78
79     if (initialized == FALSE) {
80         /* try unitialized object */
81         rc=IDirectSound8_GetCaps(dso,0);
82         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetCaps(NULL) "
83            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
84            DXGetErrorString8(rc));
85
86         rc=IDirectSound8_GetCaps(dso,&dscaps);
87         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetCaps() "
88            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
89            DXGetErrorString8(rc));
90
91         rc=IDirectSound8_Compact(dso);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_Compact() "
93            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
94            DXGetErrorString8(rc));
95
96         rc=IDirectSound8_GetSpeakerConfig(dso,&speaker_config);
97         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_GetSpeakerConfig() "
98            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
99            DXGetErrorString8(rc));
100
101         rc=IDirectSound8_VerifyCertification(dso, &certified);
102         ok(rc==DSERR_UNINITIALIZED,"IDirectSound8_VerifyCertification() "
103            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
104            DXGetErrorString8(rc));
105
106         rc=IDirectSound8_Initialize(dso,lpGuid);
107         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
108            "IDirectSound8_Initialize() failed: %s\n",DXGetErrorString8(rc));
109         if (rc==DSERR_NODRIVER) {
110             trace("  No Driver\n");
111             goto EXIT;
112         } else if (rc==E_FAIL) {
113             trace("  No Device\n");
114             goto EXIT;
115         } else if (rc==DSERR_ALLOCATED) {
116             trace("  Already In Use\n");
117             goto EXIT;
118         }
119     }
120
121     rc=IDirectSound8_Initialize(dso,lpGuid);
122     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound8_Initialize() "
123        "should have returned DSERR_ALREADYINITIALIZED: %s\n",
124        DXGetErrorString8(rc));
125
126     /* DSOUND: Error: Invalid caps buffer */
127     rc=IDirectSound8_GetCaps(dso,0);
128     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetCaps() "
129        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
130        DXGetErrorString8(rc));
131
132     ZeroMemory(&dscaps, sizeof(dscaps));
133
134     /* DSOUND: Error: Invalid caps buffer */
135     rc=IDirectSound8_GetCaps(dso,&dscaps);
136     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetCaps() "
137        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
138        DXGetErrorString8(rc));
139
140     dscaps.dwSize=sizeof(dscaps);
141
142     /* DSOUND: Running on a certified driver */
143     rc=IDirectSound8_GetCaps(dso,&dscaps);
144     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
145
146     rc=IDirectSound8_Compact(dso);
147     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound8_Compact() failed: %s\n",
148        DXGetErrorString8(rc));
149
150     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
151     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
152        DXGetErrorString8(rc));
153
154     rc=IDirectSound8_Compact(dso);
155     ok(rc==DS_OK,"IDirectSound8_Compact() failed: %s\n",DXGetErrorString8(rc));
156
157     rc=IDirectSound8_GetSpeakerConfig(dso,0);
158     ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_GetSpeakerConfig(NULL) "
159        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
160        DXGetErrorString8(rc));
161
162     rc=IDirectSound8_GetSpeakerConfig(dso,&speaker_config);
163     ok(rc==DS_OK,"IDirectSound8_GetSpeakerConfig() failed: %s\n",
164        DXGetErrorString8(rc));
165
166     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
167                                         DSSPEAKER_GEOMETRY_WIDE);
168     rc=IDirectSound8_SetSpeakerConfig(dso,speaker_config);
169     ok(rc==DS_OK,"IDirectSound8_SetSpeakerConfig() failed: %s\n",
170        DXGetErrorString8(rc));
171     if (rc==DS_OK) {
172         rc=IDirectSound8_GetSpeakerConfig(dso,&new_speaker_config);
173         ok(rc==DS_OK,"IDirectSound8_GetSpeakerConfig() failed: %s\n",
174            DXGetErrorString8(rc));
175         if (rc==DS_OK && speaker_config!=new_speaker_config)
176                trace("IDirectSound8_GetSpeakerConfig() failed to set speaker "
177                "config: expected 0x%08x, got 0x%08x\n",
178                speaker_config,new_speaker_config);
179     }
180
181     rc=IDirectSound8_VerifyCertification(dso, &certified);
182     ok(rc==DS_OK||rc==E_NOTIMPL,"IDirectSound8_VerifyCertification() failed: %s\n",
183        DXGetErrorString8(rc));
184
185 EXIT:
186     ref=IDirectSound8_Release(dso);
187     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
188 }
189
190 static void IDirectSound8_tests(void)
191 {
192     HRESULT rc;
193     LPDIRECTSOUND8 dso=NULL;
194     LPCLASSFACTORY cf=NULL;
195
196     trace("Testing IDirectSound8\n");
197
198     rc=CoGetClassObject(&CLSID_DirectSound8, CLSCTX_INPROC_SERVER, NULL,
199                         &IID_IClassFactory, (void**)&cf);
200     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound8, IID_IClassFactory) "
201        "failed: %s\n", DXGetErrorString8(rc));
202
203     rc=CoGetClassObject(&CLSID_DirectSound8, CLSCTX_INPROC_SERVER, NULL,
204                         &IID_IUnknown, (void**)&cf);
205     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound8, IID_IUnknown) "
206        "failed: %s\n", DXGetErrorString8(rc));
207
208     /* try the COM class factory method of creation with no device specified */
209     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
210                         &IID_IDirectSound8, (void**)&dso);
211     ok(rc==S_OK||rc==REGDB_E_CLASSNOTREG,"CoCreateInstance() failed: %s\n",
212        DXGetErrorString8(rc));
213     if (rc==REGDB_E_CLASSNOTREG) {
214         trace("  Class Not Registered\n");
215         return;
216     }
217     if (dso)
218         IDirectSound8_test(dso, FALSE, NULL);
219
220     /* try the COM class factory method of creation with default playback
221      *  device specified */
222     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
223                         &IID_IDirectSound8, (void**)&dso);
224     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound8) failed: %s\n",
225        DXGetErrorString8(rc));
226     if (dso)
227         IDirectSound8_test(dso, FALSE, &DSDEVID_DefaultPlayback);
228
229     /* try the COM class factory method of creation with default voice
230      * playback device specified */
231     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
232                         &IID_IDirectSound8, (void**)&dso);
233     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound8) failed: %s\n",
234        DXGetErrorString8(rc));
235     if (dso)
236         IDirectSound8_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
237
238     /* try the COM class factory method of creation with a bad
239      * IID specified */
240     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
241                         &CLSID_DirectSoundPrivate, (void**)&dso);
242     ok(rc==E_NOINTERFACE,
243        "CoCreateInstance(CLSID_DirectSound8,CLSID_DirectSoundPrivate) "
244        "should have failed: %s\n",DXGetErrorString8(rc));
245
246     /* try the COM class factory method of creation with a bad
247      * GUID and IID specified */
248     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
249                         &IID_IDirectSound8, (void**)&dso);
250     ok(rc==REGDB_E_CLASSNOTREG,
251        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound8) "
252        "should have failed: %s\n",DXGetErrorString8(rc));
253
254     /* try with no device specified */
255     rc=pDirectSoundCreate8(NULL,&dso,NULL);
256     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
257        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
258     if (rc==DS_OK && dso)
259         IDirectSound8_test(dso, TRUE, NULL);
260
261     /* try with default playback device specified */
262     rc=pDirectSoundCreate8(&DSDEVID_DefaultPlayback,&dso,NULL);
263     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
264        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
265     if (rc==DS_OK && dso)
266         IDirectSound8_test(dso, TRUE, NULL);
267
268     /* try with default voice playback device specified */
269     rc=pDirectSoundCreate8(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
270     ok(rc==S_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
271        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
272     if (rc==DS_OK && dso)
273         IDirectSound8_test(dso, TRUE, NULL);
274
275     /* try with a bad device specified */
276     rc=pDirectSoundCreate8(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
277     ok(rc==DSERR_NODRIVER,"DirectSoundCreate8(DSDEVID_DefaultVoiceCapture) "
278        "should have failed: %s\n",DXGetErrorString8(rc));
279 }
280
281 static HRESULT test_dsound8(LPGUID lpGuid)
282 {
283     HRESULT rc;
284     LPDIRECTSOUND8 dso=NULL;
285     int ref;
286
287     /* DSOUND: Error: Invalid interface buffer */
288     rc=pDirectSoundCreate8(lpGuid,0,NULL);
289     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate8() should have returned "
290        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
291
292     /* Create the DirectSound8 object */
293     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
294     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
295        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
296     if (rc!=DS_OK)
297         return rc;
298
299     /* Try the enumerated device */
300     IDirectSound8_test(dso, TRUE, lpGuid);
301
302     /* Try the COM class factory method of creation with enumerated device */
303     rc=CoCreateInstance(&CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER,
304                         &IID_IDirectSound8, (void**)&dso);
305     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
306        DXGetErrorString8(rc));
307     if (dso)
308         IDirectSound8_test(dso, FALSE, lpGuid);
309
310     /* Create a DirectSound8 object */
311     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
312     ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
313     if (rc==DS_OK) {
314         LPDIRECTSOUND8 dso1=NULL;
315
316         /* Create a second DirectSound8 object */
317         rc=pDirectSoundCreate8(lpGuid,&dso1,NULL);
318         ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
319         if (rc==DS_OK) {
320             /* Release the second DirectSound8 object */
321             ref=IDirectSound8_Release(dso1);
322             ok(ref==0,"IDirectSound8_Release() has %d references, "
323                "should have 0\n",ref);
324             ok(dso!=dso1,"DirectSound8 objects should be unique: "
325                "dso=%p,dso1=%p\n",dso,dso1);
326         }
327
328         /* Release the first DirectSound8 object */
329         ref=IDirectSound8_Release(dso);
330         ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",
331            ref);
332         if (ref!=0)
333             return DSERR_GENERIC;
334     } else
335         return rc;
336
337     /* Create a DirectSound8 object */
338     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
339     ok(rc==DS_OK,"DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
340     if (rc==DS_OK) {
341         LPDIRECTSOUNDBUFFER secondary;
342         DSBUFFERDESC bufdesc;
343         WAVEFORMATEX wfx;
344
345         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
346         ZeroMemory(&bufdesc, sizeof(bufdesc));
347         bufdesc.dwSize=sizeof(bufdesc);
348         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
349         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
350                                     wfx.nBlockAlign);
351         bufdesc.lpwfxFormat=&wfx;
352         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
353         ok(rc==DS_OK && secondary!=NULL,
354            "IDirectSound8_CreateSoundBuffer() failed to create a secondary "
355            "buffer: %s\n",DXGetErrorString8(rc));
356         if (rc==DS_OK && secondary!=NULL) {
357             LPDIRECTSOUND3DBUFFER buffer3d;
358             LPDIRECTSOUNDBUFFER8 buffer8;
359             rc=IDirectSound8_QueryInterface(secondary,
360                                             &IID_IDirectSound3DBuffer,
361                                             (void **)&buffer3d);
362             ok(rc==DS_OK && buffer3d!=NULL,
363                "IDirectSound8_QueryInterface() failed: %s\n",
364                DXGetErrorString8(rc));
365             if (rc==DS_OK && buffer3d!=NULL) {
366                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
367                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
368                    "should have 2\n",ref);
369             }
370             rc=IDirectSound8_QueryInterface(secondary,
371                                             &IID_IDirectSoundBuffer8,
372                                             (void **)&buffer8);
373             if (rc==DS_OK && buffer8!=NULL) {
374                 ref=IDirectSoundBuffer8_AddRef(buffer8);
375                 ok(ref==3,"IDirectSoundBuffer8_AddRef() has %d references, "
376                    "should have 3\n",ref);
377             }
378             ref=IDirectSoundBuffer_AddRef(secondary);
379             ok(ref==4,"IDirectSoundBuffer_AddRef() has %d references, "
380                "should have 4\n",ref);
381         }
382         /* release with buffer */
383         ref=IDirectSound8_Release(dso);
384         ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",
385            ref);
386         if (ref!=0)
387             return DSERR_GENERIC;
388     } else
389         return rc;
390
391     return DS_OK;
392 }
393
394 static HRESULT test_primary8(LPGUID lpGuid)
395 {
396     HRESULT rc;
397     LPDIRECTSOUND8 dso=NULL;
398     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=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         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
500         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
501            DXGetErrorString8(rc));
502
503         if (winetest_interactive) {
504             trace("Playing a 5 seconds reference tone at the current volume.\n");
505             if (rc==DS_OK)
506                 trace("(the current volume is %d according to DirectSound)\n",
507                       vol);
508             trace("All subsequent tones should be identical to this one.\n");
509             trace("Listen for stutter, changes in pitch, volume, etc.\n");
510         }
511         test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
512                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0);
513
514         ref=IDirectSoundBuffer_Release(primary);
515         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
516            "should have 0\n",ref);
517     }
518
519     /* Set the CooperativeLevel back to normal */
520     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
521     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
522     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
523        DXGetErrorString8(rc));
524
525 EXIT:
526     ref=IDirectSound8_Release(dso);
527     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
528     if (ref!=0)
529         return DSERR_GENERIC;
530
531     return rc;
532 }
533
534 /*
535  * Test the primary buffer at different formats while keeping the
536  * secondary buffer at a constant format.
537  */
538 static HRESULT test_primary_secondary8(LPGUID lpGuid)
539 {
540     HRESULT rc;
541     LPDIRECTSOUND8 dso=NULL;
542     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
543     DSBUFFERDESC bufdesc;
544     DSCAPS dscaps;
545     WAVEFORMATEX wfx, wfx2;
546     int ref;
547     unsigned int f;
548
549     /* Create the DirectSound object */
550     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
551     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
552        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
553     if (rc!=DS_OK)
554         return rc;
555
556     /* Get the device capabilities */
557     ZeroMemory(&dscaps, sizeof(dscaps));
558     dscaps.dwSize=sizeof(dscaps);
559     rc=IDirectSound8_GetCaps(dso,&dscaps);
560     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
561     if (rc!=DS_OK)
562         goto EXIT;
563
564     /* We must call SetCooperativeLevel before creating primary buffer */
565     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
566     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
567     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
568        DXGetErrorString8(rc));
569     if (rc!=DS_OK)
570         goto EXIT;
571
572     ZeroMemory(&bufdesc, sizeof(bufdesc));
573     bufdesc.dwSize=sizeof(bufdesc);
574     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
575     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
576     ok(rc==DS_OK && primary!=NULL,
577        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
578        "%s\n",DXGetErrorString8(rc));
579
580     if (rc==DS_OK && primary!=NULL) {
581         for (f=0;f<NB_FORMATS;f++) {
582             /* We must call SetCooperativeLevel to be allowed to call
583              * SetFormat */
584             /* DSOUND: Setting DirectSound cooperative level to
585              * DSSCL_PRIORITY */
586             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
587             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
588                DXGetErrorString8(rc));
589             if (rc!=DS_OK)
590                 goto EXIT;
591
592             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
593                         formats[f][2]);
594             wfx2=wfx;
595             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
596             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
597                format_string(&wfx), DXGetErrorString8(rc));
598
599             /* There is no garantee that SetFormat will actually change the
600              * format to what we asked for. It depends on what the soundcard
601              * supports. So we must re-query the format.
602              */
603             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
604             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
605                DXGetErrorString8(rc));
606             if (rc==DS_OK &&
607                 (wfx.wFormatTag!=wfx2.wFormatTag ||
608                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
609                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
610                  wfx.nChannels!=wfx2.nChannels)) {
611                 trace("Requested primary format tag=0x%04x %dx%dx%d "
612                       "avg.B/s=%d align=%d\n",
613                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
614                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
615                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
616                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
617                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
618             }
619
620             /* Set the CooperativeLevel back to normal */
621             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
622             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
623             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
624                DXGetErrorString8(rc));
625
626             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
627
628             secondary=NULL;
629             ZeroMemory(&bufdesc, sizeof(bufdesc));
630             bufdesc.dwSize=sizeof(bufdesc);
631             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
632             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
633                                         wfx.nBlockAlign);
634             bufdesc.lpwfxFormat=&wfx2;
635             if (winetest_interactive) {
636                 trace("  Testing a primary buffer at %dx%dx%d with a "
637                       "secondary buffer at %dx%dx%d\n",
638                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
639                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
640             }
641             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
642             ok(rc==DS_OK && secondary!=NULL,
643                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
644                "buffer %s\n",DXGetErrorString8(rc));
645
646             if (rc==DS_OK && secondary!=NULL) {
647                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
648                              winetest_interactive,1.0,0,NULL,0,0);
649
650                 ref=IDirectSoundBuffer_Release(secondary);
651                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
652                    "should have 0\n",ref);
653             }
654         }
655
656         ref=IDirectSoundBuffer_Release(primary);
657         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
658            "should have 0\n",ref);
659     }
660
661     /* Set the CooperativeLevel back to normal */
662     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
663     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
664     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
665        DXGetErrorString8(rc));
666
667 EXIT:
668     ref=IDirectSound8_Release(dso);
669     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
670     if (ref!=0)
671         return DSERR_GENERIC;
672
673     return rc;
674 }
675
676 static HRESULT test_secondary8(LPGUID lpGuid)
677 {
678     HRESULT rc;
679     LPDIRECTSOUND8 dso=NULL;
680     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
681     DSBUFFERDESC bufdesc;
682     DSCAPS dscaps;
683     WAVEFORMATEX wfx, wfx1;
684     DWORD f;
685     int ref;
686
687     /* Create the DirectSound object */
688     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
689     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
690        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
691     if (rc!=DS_OK)
692         return rc;
693
694     /* Get the device capabilities */
695     ZeroMemory(&dscaps, sizeof(dscaps));
696     dscaps.dwSize=sizeof(dscaps);
697     rc=IDirectSound8_GetCaps(dso,&dscaps);
698     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
699     if (rc!=DS_OK)
700         goto EXIT;
701
702     /* We must call SetCooperativeLevel before creating primary buffer */
703     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
704     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
705     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
706        DXGetErrorString8(rc));
707     if (rc!=DS_OK)
708         goto EXIT;
709
710     ZeroMemory(&bufdesc, sizeof(bufdesc));
711     bufdesc.dwSize=sizeof(bufdesc);
712     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
713     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
714     ok(rc==DS_OK && primary!=NULL,
715        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer "
716        "%s\n",DXGetErrorString8(rc));
717
718     if (rc==DS_OK && primary!=NULL) {
719         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
720         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
721            DXGetErrorString8(rc));
722         if (rc!=DS_OK)
723             goto EXIT1;
724
725         for (f=0;f<NB_FORMATS;f++) {
726             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
727                         formats[f][2]);
728             secondary=NULL;
729             ZeroMemory(&bufdesc, sizeof(bufdesc));
730             bufdesc.dwSize=sizeof(bufdesc);
731             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
732             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
733                                         wfx.nBlockAlign);
734             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
735             ok(rc==DSERR_INVALIDPARAM,"IDirectSound8_CreateSoundBuffer() "
736                "should have returned DSERR_INVALIDPARAM, returned: %s\n",
737                DXGetErrorString8(rc));
738             if (rc==DS_OK && secondary!=NULL)
739                 IDirectSoundBuffer_Release(secondary);
740
741             secondary=NULL;
742             ZeroMemory(&bufdesc, sizeof(bufdesc));
743             bufdesc.dwSize=sizeof(bufdesc);
744             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
745             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
746                                         wfx.nBlockAlign);
747             bufdesc.lpwfxFormat=&wfx;
748             if (winetest_interactive) {
749                 trace("  Testing a secondary buffer at %dx%dx%d "
750                       "with a primary buffer at %dx%dx%d\n",
751                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
752                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
753             }
754             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
755             ok(rc==DS_OK && secondary!=NULL,
756                "IDirectSound8_CreateSoundBuffer() failed to create a secondary "
757                "buffer: %s\n",DXGetErrorString8(rc));
758
759             if (rc==DS_OK && secondary!=NULL) {
760                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
761                              winetest_interactive,1.0,0,NULL,0,0);
762
763                 ref=IDirectSoundBuffer_Release(secondary);
764                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
765                    "should have 0\n",ref);
766             }
767         }
768 EXIT1:
769         ref=IDirectSoundBuffer_Release(primary);
770         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
771            "should have 0\n",ref);
772     }
773
774     /* Set the CooperativeLevel back to normal */
775     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
776     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
777     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel() failed: %s\n",
778        DXGetErrorString8(rc));
779
780 EXIT:
781     ref=IDirectSound8_Release(dso);
782     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
783     if (ref!=0)
784         return DSERR_GENERIC;
785
786     return rc;
787 }
788
789 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
790                                    LPCSTR lpcstrModule, LPVOID lpContext)
791 {
792     HRESULT rc;
793     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
794     rc = test_dsound8(lpGuid);
795     if (rc == DSERR_NODRIVER)
796         trace("  No Driver\n");
797     else if (rc == DSERR_ALLOCATED)
798         trace("  Already In Use\n");
799     else if (rc == E_FAIL)
800         trace("  No Device\n");
801     else {
802         test_primary8(lpGuid);
803         test_primary_secondary8(lpGuid);
804         test_secondary8(lpGuid);
805     }
806
807     return 1;
808 }
809
810 static void dsound8_tests(void)
811 {
812     HRESULT rc;
813     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
814     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
815 }
816
817 const char * get_file_version(const char * file_name)
818 {
819     static char version[32];
820     static char backslash[] = "\\";
821     DWORD size;
822     DWORD handle;
823
824     size = GetFileVersionInfoSizeA("dsound.dll", &handle);
825     if (size) {
826         char * data = HeapAlloc(GetProcessHeap(), 0, size);
827         if (data) {
828             if (GetFileVersionInfoA("dsound.dll", handle, size, data)) {
829                 VS_FIXEDFILEINFO *pFixedVersionInfo;
830                 UINT len;
831                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
832                     sprintf(version, "%d.%d.%d.%d",
833                             pFixedVersionInfo->dwFileVersionMS >> 16,
834                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
835                             pFixedVersionInfo->dwFileVersionLS >> 16,
836                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
837                 } else
838                     sprintf(version, "not available");
839             } else
840                 sprintf(version, "failed");
841
842             HeapFree(GetProcessHeap(), 0, data);
843         } else
844             sprintf(version, "failed");
845     } else
846         sprintf(version, "not available");
847
848     return version;
849 }
850
851 START_TEST(dsound8)
852 {
853     HMODULE hDsound;
854
855     CoInitialize(NULL);
856
857     hDsound = LoadLibraryA("dsound.dll");
858     if (!hDsound) {
859         trace("dsound.dll not found\n");
860         return;
861     }
862
863     trace("DLL Version: %s\n", get_file_version("dsound.dll"));
864
865     pDirectSoundCreate8 = (void*)GetProcAddress(hDsound, "DirectSoundCreate8");
866     if (!pDirectSoundCreate8) {
867         trace("dsound8 test skipped\n");
868         return;
869     }
870
871     IDirectSound8_tests();
872     dsound8_tests();
873
874     CoUninitialize();
875 }