Declaration, implemention and test for BuildTrusteeWithSid.
[wine] / dlls / dsound / tests / dsound.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include <windows.h>
31
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "wine/test.h"
36 #include "windef.h"
37 #include "wingdi.h"
38 #include "dsound.h"
39 #include "dxerr8.h"
40 #include "dsconf.h"
41
42 #include "dsound_test.h"
43
44 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
45                               LPCGUID lpGuid)
46 {
47     HRESULT rc;
48     DSCAPS dscaps;
49     int ref;
50     IUnknown * unknown;
51     IDirectSound * ds;
52     IDirectSound8 * ds8;
53     DWORD speaker_config, new_speaker_config;
54
55     /* Try to Query for objects */
56     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
57     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %s\n",
58        DXGetErrorString8(rc));
59     if (rc==DS_OK)
60         IDirectSound_Release(unknown);
61
62     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
63     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %s\n",
64        DXGetErrorString8(rc));
65     if (rc==DS_OK)
66         IDirectSound_Release(ds);
67
68     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
69     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
70        "should have failed: %s\n",DXGetErrorString8(rc));
71     if (rc==DS_OK)
72         IDirectSound8_Release(ds8);
73
74     if (initialized == FALSE) {
75         /* try unitialized object */
76         rc=IDirectSound_GetCaps(dso,0);
77         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
78            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
79            DXGetErrorString8(rc));
80
81         rc=IDirectSound_GetCaps(dso,&dscaps);
82         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
83            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
84            DXGetErrorString8(rc));
85
86         rc=IDirectSound_Compact(dso);
87         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
88            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
89            DXGetErrorString8(rc));
90
91         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
93            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
94            DXGetErrorString8(rc));
95
96         rc=IDirectSound_Initialize(dso,lpGuid);
97         ok(rc==DS_OK,"IDirectSound_Initialize() failed: %s\n",
98            DXGetErrorString8(rc));
99     }
100
101     /* DSOUND: Error: Invalid caps buffer */
102     rc=IDirectSound_GetCaps(dso,0);
103     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
104        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
105        DXGetErrorString8(rc));
106
107     ZeroMemory(&dscaps, sizeof(dscaps));
108
109     /* DSOUND: Error: Invalid caps buffer */
110     rc=IDirectSound_GetCaps(dso,&dscaps);
111     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
112        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
113        DXGetErrorString8(rc));
114
115     dscaps.dwSize=sizeof(dscaps);
116
117     /* DSOUND: Running on a certified driver */
118     rc=IDirectSound_GetCaps(dso,&dscaps);
119     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
120
121     rc=IDirectSound_Compact(dso);
122     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %s\n",
123        DXGetErrorString8(rc));
124
125     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
126     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
127        DXGetErrorString8(rc));
128
129     rc=IDirectSound_Compact(dso);
130     ok(rc==DS_OK,"IDirectSound_Compact() failed: %s\n",DXGetErrorString8(rc));
131
132     rc=IDirectSound_GetSpeakerConfig(dso,0);
133     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
134        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
135        DXGetErrorString8(rc));
136
137     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
138     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
139        DXGetErrorString8(rc));
140
141     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
142                                         DSSPEAKER_GEOMETRY_WIDE);
143     rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
144     ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %s\n",
145        DXGetErrorString8(rc));
146     if (rc==DS_OK) {
147         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
148         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
149            DXGetErrorString8(rc));
150         if (rc==DS_OK)
151             ok(speaker_config==new_speaker_config,
152                "IDirectSound_GetSpeakerConfig() failed to set speaker config: "
153                "expected 0x%08lx, got 0x%08lx\n",
154                speaker_config,new_speaker_config);
155     }
156
157     ref=IDirectSound_Release(dso);
158     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
159 }
160
161 static void IDirectSound_tests()
162 {
163     HRESULT rc;
164     LPDIRECTSOUND dso=NULL;
165
166     trace("Testing IDirectSound\n");
167
168     /* try the COM class factory method of creation with no device specified */
169     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
170                         &IID_IDirectSound, (void**)&dso);
171     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
172        DXGetErrorString8(rc));
173     if (dso)
174         IDirectSound_test(dso, FALSE, NULL);
175
176     /* try the COM class factory method of creation with default playback
177      * device specified */
178     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
179                         &IID_IDirectSound, (void**)&dso);
180     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
181        DXGetErrorString8(rc));
182     if (dso)
183         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
184
185     /* try the COM class factory method of creation with default voice
186      * playback device specified */
187     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
188                         &IID_IDirectSound, (void**)&dso);
189     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
190        DXGetErrorString8(rc));
191     if (dso)
192         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
193
194     /* try the COM class factory method of creation with a bad
195      * IID specified */
196     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
197                         &CLSID_DirectSoundPrivate, (void**)&dso);
198     ok(rc==E_NOINTERFACE,
199        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
200        "should have failed: %s\n",DXGetErrorString8(rc));
201
202     /* try the COM class factory method of creation with a bad
203      * GUID and IID specified */
204     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
205                         &IID_IDirectSound, (void**)&dso);
206     ok(rc==REGDB_E_CLASSNOTREG,
207        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
208        "should have failed: %s\n",DXGetErrorString8(rc));
209
210     /* try with no device specified */
211     rc=DirectSoundCreate(NULL,&dso,NULL);
212     ok(rc==S_OK,"DirectSoundCreate(NULL) failed: %s\n",DXGetErrorString8(rc));
213     if (dso)
214         IDirectSound_test(dso, TRUE, NULL);
215
216     /* try with default playback device specified */
217     rc=DirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
218     ok(rc==S_OK,"DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %s\n",
219        DXGetErrorString8(rc));
220     if (dso)
221         IDirectSound_test(dso, TRUE, NULL);
222
223     /* try with default voice playback device specified */
224     rc=DirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
225     ok(rc==S_OK,"DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %s\n",
226        DXGetErrorString8(rc));
227     if (dso)
228         IDirectSound_test(dso, TRUE, NULL);
229
230     /* try with a bad device specified */
231     rc=DirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
232     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
233        "should have failed: %s\n",DXGetErrorString8(rc));
234 }
235
236 static HRESULT test_dsound(LPGUID lpGuid)
237 {
238     HRESULT rc;
239     LPDIRECTSOUND dso=NULL;
240     int ref;
241
242     /* DSOUND: Error: Invalid interface buffer */
243     rc=DirectSoundCreate(lpGuid,0,NULL);
244     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
245        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
246
247     /* Create the DirectSound object */
248     rc=DirectSoundCreate(lpGuid,&dso,NULL);
249     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
250     if (rc!=DS_OK)
251         return rc;
252
253     /* Try the enumerated device */
254     IDirectSound_test(dso, TRUE, lpGuid);
255
256     /* Try the COM class factory method of creation with enumerated device */
257     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
258                         &IID_IDirectSound, (void**)&dso);
259     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
260        DXGetErrorString8(rc));
261     if (dso)
262         IDirectSound_test(dso, FALSE, lpGuid);
263
264     /* Create a DirectSound object */
265     rc=DirectSoundCreate(lpGuid,&dso,NULL);
266     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
267     if (rc==DS_OK) {
268         LPDIRECTSOUND dso1=NULL;
269
270         /* Create a second DirectSound object */
271         rc=DirectSoundCreate(lpGuid,&dso1,NULL);
272         ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
273         if (rc==DS_OK) {
274             /* Release the second DirectSound object */
275             ref=IDirectSound_Release(dso1);
276             ok(ref==0,"IDirectSound_Release() has %d references, should have "
277                "0\n",ref);
278             ok(dso!=dso1,"DirectSound objects should be unique: "
279                "dso=0x%08lx,dso1=0x%08lx\n",(DWORD)dso,(DWORD)dso1);
280         }
281
282         /* Release the first DirectSound object */
283         ref=IDirectSound_Release(dso);
284         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
285            ref);
286         if (ref!=0)
287             return DSERR_GENERIC;
288     } else
289         return rc;
290
291     /* Create a DirectSound object */
292     rc=DirectSoundCreate(lpGuid,&dso,NULL);
293     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
294     if (rc==DS_OK) {
295         LPDIRECTSOUNDBUFFER secondary;
296         DSBUFFERDESC bufdesc;
297         WAVEFORMATEX wfx;
298
299         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
300         ZeroMemory(&bufdesc, sizeof(bufdesc));
301         bufdesc.dwSize=sizeof(bufdesc);
302         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
303         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
304         bufdesc.lpwfxFormat=&wfx;
305         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
306         ok(rc==DS_OK && secondary!=NULL,
307            "IDirectSound_CreateSoundBuffer() failed to create a secondary "
308            "buffer %s\n",DXGetErrorString8(rc));
309         if (rc==DS_OK && secondary!=NULL) {
310             LPDIRECTSOUND3DBUFFER buffer3d;
311             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
312                                            (void **)&buffer3d);
313             ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
314                "failed:  %s\n",DXGetErrorString8(rc));
315             if (rc==DS_OK && buffer3d!=NULL) {
316                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
317                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
318                    "should have 2\n",ref);
319             }
320             ref=IDirectSoundBuffer_AddRef(secondary);
321             ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
322                "should have 2\n",ref);
323         }
324         /* release with buffer */
325         ref=IDirectSound_Release(dso);
326         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
327            ref);
328         if (ref!=0)
329             return DSERR_GENERIC;
330     } else
331         return rc;
332
333     return DS_OK;
334 }
335
336 static HRESULT test_primary(LPGUID lpGuid)
337 {
338     HRESULT rc;
339     LPDIRECTSOUND dso=NULL;
340     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
341     DSBUFFERDESC bufdesc;
342     DSCAPS dscaps;
343     int ref;
344
345     /* Create the DirectSound object */
346     rc=DirectSoundCreate(lpGuid,&dso,NULL);
347     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
348     if (rc!=DS_OK)
349         return rc;
350
351     /* Get the device capabilities */
352     ZeroMemory(&dscaps, sizeof(dscaps));
353     dscaps.dwSize=sizeof(dscaps);
354     rc=IDirectSound_GetCaps(dso,&dscaps);
355     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
356     if (rc!=DS_OK)
357         goto EXIT;
358
359     /* DSOUND: Error: Invalid buffer description pointer */
360     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
361     ok(rc==DSERR_INVALIDPARAM,
362        "IDirectSound_CreateSoundBuffer() should have failed: %s\n",
363        DXGetErrorString8(rc));
364
365     /* DSOUND: Error: Invalid buffer description pointer */
366     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
367     ok(rc==DSERR_INVALIDPARAM && primary==0,
368        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
369        "dsbo=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
370
371     /* DSOUND: Error: Invalid buffer description pointer */
372     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,0,NULL);
373     ok(rc==DSERR_INVALIDPARAM && primary==0,
374        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
375        "dsbo=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
376
377     ZeroMemory(&bufdesc, sizeof(bufdesc));
378
379     /* DSOUND: Error: Invalid size */
380     /* DSOUND: Error: Invalid buffer description */
381     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
382     ok(rc==DSERR_INVALIDPARAM && primary==0,
383        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
384        "primary=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
385
386     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
387     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
388     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
389     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
390        DXGetErrorString8(rc));
391     if (rc!=DS_OK)
392         goto EXIT;
393
394     /* Testing the primary buffer */
395     primary=NULL;
396     ZeroMemory(&bufdesc, sizeof(bufdesc));
397     bufdesc.dwSize=sizeof(bufdesc);
398     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
399     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
400     ok(rc==DS_OK && primary!=NULL,
401        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: "
402        "%s\n",DXGetErrorString8(rc));
403     if (rc==DS_OK && primary!=NULL) {
404         LONG vol;
405
406         /* Try to create a second primary buffer */
407         /* DSOUND: Error: The primary buffer already exists.
408          * Any changes made to the buffer description will be ignored. */
409         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
410         ok(rc==DS_OK && second==primary,
411            "IDirectSound_CreateSoundBuffer() should have returned original "
412            "primary buffer: %s\n",DXGetErrorString8(rc));
413         ref=IDirectSoundBuffer_Release(second);
414         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
415            "should have 1\n",ref);
416
417         /* Try to duplicate a primary buffer */
418         /* DSOUND: Error: Can't duplicate primary buffers */
419         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
420         /* rc=0x88780032 */
421         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
422            "should have failed %s\n",DXGetErrorString8(rc));
423
424         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
425         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
426            DXGetErrorString8(rc));
427
428         if (winetest_interactive) {
429             trace("Playing a 5 seconds reference tone at the current volume.\n");
430             if (rc==DS_OK)
431                 trace("(the current volume is %ld according to DirectSound)\n",
432                       vol);
433             trace("All subsequent tones should be identical to this one.\n");
434             trace("Listen for stutter, changes in pitch, volume, etc.\n");
435         }
436         test_buffer(dso,primary,1,FALSE,0,FALSE,0,winetest_interactive &&
437                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0);
438
439         ref=IDirectSoundBuffer_Release(primary);
440         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
441            "should have 0\n",ref);
442     }
443
444     /* Set the CooperativeLevel back to normal */
445     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
446     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
447     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
448        DXGetErrorString8(rc));
449
450 EXIT:
451     ref=IDirectSound_Release(dso);
452     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
453     if (ref!=0)
454         return DSERR_GENERIC;
455
456     return rc;
457 }
458
459 /*
460  * Test the primary buffer at different formats while keeping the
461  * secondary buffer at a constant format.
462  */
463 static HRESULT test_primary_secondary(LPGUID lpGuid)
464 {
465     HRESULT rc;
466     LPDIRECTSOUND dso=NULL;
467     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
468     DSBUFFERDESC bufdesc;
469     DSCAPS dscaps;
470     WAVEFORMATEX wfx, wfx2;
471     int f,ref;
472
473     /* Create the DirectSound object */
474     rc=DirectSoundCreate(lpGuid,&dso,NULL);
475     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
476     if (rc!=DS_OK)
477         return rc;
478
479     /* Get the device capabilities */
480     ZeroMemory(&dscaps, sizeof(dscaps));
481     dscaps.dwSize=sizeof(dscaps);
482     rc=IDirectSound_GetCaps(dso,&dscaps);
483     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
484     if (rc!=DS_OK)
485         goto EXIT;
486
487     /* We must call SetCooperativeLevel before creating primary buffer */
488     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
489     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
490     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
491        DXGetErrorString8(rc));
492     if (rc!=DS_OK)
493         goto EXIT;
494
495     ZeroMemory(&bufdesc, sizeof(bufdesc));
496     bufdesc.dwSize=sizeof(bufdesc);
497     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
498     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
499     ok(rc==DS_OK && primary!=NULL,
500        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
501        "%s\n",DXGetErrorString8(rc));
502
503     if (rc==DS_OK && primary!=NULL) {
504         for (f=0;f<NB_FORMATS;f++) {
505             /* We must call SetCooperativeLevel to be allowed to call SetFormat */
506             /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
507             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
508             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
509                DXGetErrorString8(rc));
510             if (rc!=DS_OK)
511                 goto EXIT;
512
513             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
514                         formats[f][2]);
515             wfx2=wfx;
516             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
517             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat() failed: %s\n",
518                DXGetErrorString8(rc));
519
520             /* There is no garantee that SetFormat will actually change the
521              * format to what we asked for. It depends on what the soundcard
522              * supports. So we must re-query the format.
523              */
524             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
525             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
526                DXGetErrorString8(rc));
527             if (rc==DS_OK &&
528                 (wfx.wFormatTag!=wfx2.wFormatTag ||
529                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
530                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
531                  wfx.nChannels!=wfx2.nChannels)) {
532                 trace("Requested primary format tag=0x%04x %ldx%dx%d "
533                       "avg.B/s=%ld align=%d\n",
534                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
535                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
536                 trace("Got tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
537                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
538                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
539             }
540
541             /* Set the CooperativeLevel back to normal */
542             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
543             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
544             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
545                DXGetErrorString8(rc));
546
547             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
548
549             secondary=NULL;
550             ZeroMemory(&bufdesc, sizeof(bufdesc));
551             bufdesc.dwSize=sizeof(bufdesc);
552             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
553             bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
554             bufdesc.lpwfxFormat=&wfx2;
555             if (winetest_interactive) {
556                 trace("  Testing a primary buffer at %ldx%dx%d with a "
557                       "secondary buffer at %ldx%dx%d\n",
558                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
559                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
560             }
561             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
562             ok(rc==DS_OK && secondary!=NULL,
563                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
564                "buffer %s\n",DXGetErrorString8(rc));
565
566             if (rc==DS_OK && secondary!=NULL) {
567                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
568                             winetest_interactive,1.0,0,NULL,0,0);
569
570                 ref=IDirectSoundBuffer_Release(secondary);
571                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
572                    "should have 0\n",ref);
573             }
574         }
575
576         ref=IDirectSoundBuffer_Release(primary);
577         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
578            "should have 0\n",ref);
579     }
580
581     /* Set the CooperativeLevel back to normal */
582     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
583     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
584     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
585        DXGetErrorString8(rc));
586
587 EXIT:
588     ref=IDirectSound_Release(dso);
589     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
590     if (ref!=0)
591         return DSERR_GENERIC;
592
593     return rc;
594 }
595
596 static HRESULT test_secondary(LPGUID lpGuid)
597 {
598     HRESULT rc;
599     LPDIRECTSOUND dso=NULL;
600     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
601     DSBUFFERDESC bufdesc;
602     DSCAPS dscaps;
603     WAVEFORMATEX wfx;
604     DWORD f;
605     int ref;
606
607     /* Create the DirectSound object */
608     rc=DirectSoundCreate(lpGuid,&dso,NULL);
609     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
610     if (rc!=DS_OK)
611         return rc;
612
613     /* Get the device capabilities */
614     ZeroMemory(&dscaps, sizeof(dscaps));
615     dscaps.dwSize=sizeof(dscaps);
616     rc=IDirectSound_GetCaps(dso,&dscaps);
617     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
618     if (rc!=DS_OK)
619         goto EXIT;
620
621     /* We must call SetCooperativeLevel before creating primary buffer */
622     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
623     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
624     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
625        DXGetErrorString8(rc));
626     if (rc!=DS_OK)
627         goto EXIT;
628
629     ZeroMemory(&bufdesc, sizeof(bufdesc));
630     bufdesc.dwSize=sizeof(bufdesc);
631     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
632     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
633     ok(rc==DS_OK && primary!=NULL,
634        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
635        "%s\n",DXGetErrorString8(rc));
636
637     if (rc==DS_OK && primary!=NULL) {
638         for (f=0;f<NB_FORMATS;f++) {
639             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
640                         formats[f][2]);
641             secondary=NULL;
642             ZeroMemory(&bufdesc, sizeof(bufdesc));
643             bufdesc.dwSize=sizeof(bufdesc);
644             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
645             bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
646             bufdesc.lpwfxFormat=&wfx;
647             if (winetest_interactive) {
648                 trace("  Testing a secondary buffer at %ldx%dx%d\n",
649                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels);
650             }
651             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
652             ok(rc==DS_OK && secondary!=NULL,
653                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
654                "buffer %s\n",DXGetErrorString8(rc));
655
656             if (rc==DS_OK && secondary!=NULL) {
657                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
658                             winetest_interactive,1.0,0,NULL,0,0);
659
660                 ref=IDirectSoundBuffer_Release(secondary);
661                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
662                    "should have 0\n",ref);
663             }
664         }
665
666         ref=IDirectSoundBuffer_Release(primary);
667         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
668            "should have 0\n",ref);
669     }
670
671     /* Set the CooperativeLevel back to normal */
672     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
673     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
674     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
675        DXGetErrorString8(rc));
676
677 EXIT:
678     ref=IDirectSound_Release(dso);
679     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
680     if (ref!=0)
681         return DSERR_GENERIC;
682
683     return rc;
684 }
685
686 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
687                                    LPCSTR lpcstrModule, LPVOID lpContext)
688 {
689     trace("*** Testing %s - %s\n",lpcstrDescription,lpcstrModule);
690     test_dsound(lpGuid);
691     test_primary(lpGuid);
692     test_primary_secondary(lpGuid);
693     test_secondary(lpGuid);
694
695     return 1;
696 }
697
698 static void dsound_tests()
699 {
700     HRESULT rc;
701     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
702     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
703 }
704
705 START_TEST(dsound)
706 {
707     CoInitialize(NULL);
708
709     IDirectSound_tests();
710     dsound_tests();
711 }