wbemprox: Implement Win32_ComputerSystem.DomainRole.
[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  * Copyright (c) 2007 Maarten Lankhorst
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include <windows.h>
30
31 #include "wine/test.h"
32 #include "dsound.h"
33 #include "dsconf.h"
34 #include "mmreg.h"
35 #include "initguid.h"
36 #include "ks.h"
37 #include "ksmedia.h"
38
39 #include "dsound_test.h"
40
41 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
42
43 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
44 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID,LPDIRECTSOUND*,
45     LPUNKNOWN)=NULL;
46
47 static BOOL gotdx8;
48
49 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
50                               LPCGUID lpGuid)
51 {
52     HRESULT rc;
53     DSCAPS dscaps;
54     int ref;
55     IUnknown * unknown;
56     IDirectSound * ds;
57     IDirectSound8 * ds8;
58     DWORD speaker_config, new_speaker_config, ref_speaker_config;
59
60     /* Try to Query for objects */
61     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
62     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %08x\n", rc);
63     if (rc==DS_OK)
64         IDirectSound_Release(unknown);
65
66     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
67     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %08x\n", rc);
68     if (rc==DS_OK)
69         IDirectSound_Release(ds);
70
71     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
72     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
73        "should have failed: %08x\n",rc);
74     if (rc==DS_OK)
75         IDirectSound8_Release(ds8);
76
77     if (initialized == FALSE) {
78         /* try uninitialized object */
79         rc=IDirectSound_GetCaps(dso,0);
80         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
81            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
82
83         rc=IDirectSound_GetCaps(dso,&dscaps);
84         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
85            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
86
87         rc=IDirectSound_Compact(dso);
88         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
89            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
90
91         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
93            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
94
95         rc=IDirectSound_Initialize(dso,lpGuid);
96         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
97            "IDirectSound_Initialize() failed: %08x\n",rc);
98         if (rc==DSERR_NODRIVER) {
99             trace("  No Driver\n");
100             goto EXIT;
101         } else if (rc==E_FAIL) {
102             trace("  No Device\n");
103             goto EXIT;
104         } else if (rc==DSERR_ALLOCATED) {
105             trace("  Already In Use\n");
106             goto EXIT;
107         }
108     }
109
110     rc=IDirectSound_Initialize(dso,lpGuid);
111     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
112        "should have returned DSERR_ALREADYINITIALIZED: %08x\n", rc);
113
114     /* DSOUND: Error: Invalid caps buffer */
115     rc=IDirectSound_GetCaps(dso,0);
116     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
117        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
118
119     ZeroMemory(&dscaps, sizeof(dscaps));
120
121     /* DSOUND: Error: Invalid caps buffer */
122     rc=IDirectSound_GetCaps(dso,&dscaps);
123     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
124        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
125
126     dscaps.dwSize=sizeof(dscaps);
127
128     /* DSOUND: Running on a certified driver */
129     rc=IDirectSound_GetCaps(dso,&dscaps);
130     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
131
132     rc=IDirectSound_Compact(dso);
133     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %08x\n", rc);
134
135     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
136     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
137
138     rc=IDirectSound_Compact(dso);
139     ok(rc==DS_OK,"IDirectSound_Compact() failed: %08x\n",rc);
140
141     rc=IDirectSound_GetSpeakerConfig(dso,0);
142     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
143        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
144
145     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
146     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %08x\n", rc);
147     ref_speaker_config = speaker_config;
148
149     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
150                                         DSSPEAKER_GEOMETRY_WIDE);
151     if (speaker_config == ref_speaker_config)
152         speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
153                                             DSSPEAKER_GEOMETRY_NARROW);
154     if(rc==DS_OK) {
155         rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
156         ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %08x\n", rc);
157     }
158     if (rc==DS_OK) {
159         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
160         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %08x\n", rc);
161         if (rc==DS_OK && speaker_config!=new_speaker_config)
162                trace("IDirectSound_GetSpeakerConfig() failed to set speaker "
163                "config: expected 0x%08x, got 0x%08x\n",
164                speaker_config,new_speaker_config);
165         IDirectSound_SetSpeakerConfig(dso,ref_speaker_config);
166     }
167
168 EXIT:
169     ref=IDirectSound_Release(dso);
170     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
171 }
172
173 static void IDirectSound_tests(void)
174 {
175     HRESULT rc;
176     IDirectSound *dso=(IDirectSound*)0xdeadbeef;
177     LPCLASSFACTORY cf=NULL;
178
179     trace("Testing IDirectSound\n");
180
181     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
182                         &IID_IClassFactory, (void**)&cf);
183     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IClassFactory) "
184        "failed: %08x\n", rc);
185
186     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
187                         &IID_IUnknown, (void**)&cf);
188     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IUnknown) "
189        "failed: %08x\n", rc);
190
191     /* COM aggregation */
192     rc=CoCreateInstance(&CLSID_DirectSound, (IUnknown*)&dso, CLSCTX_INPROC_SERVER,
193                         &IID_IDirectSound, (void**)&dso);
194     ok(rc==CLASS_E_NOAGGREGATION || broken(rc==DSERR_INVALIDPARAM),
195        "DirectMusicPerformance create failed: %08x, expected CLASS_E_NOAGGREGATION\n", rc);
196
197     /* try the COM class factory method of creation with no device specified */
198     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
199                         &IID_IDirectSound, (void**)&dso);
200     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
201     if (dso)
202         IDirectSound_test(dso, FALSE, NULL);
203
204     /* try the COM class factory method of creation with default playback
205      * device specified */
206     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
207                         &IID_IDirectSound, (void**)&dso);
208     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
209     if (dso)
210         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
211
212     /* try the COM class factory method of creation with default voice
213      * playback device specified */
214     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
215                         &IID_IDirectSound, (void**)&dso);
216     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
217     if (dso)
218         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
219
220     /* try the COM class factory method of creation with a bad
221      * IID specified */
222     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
223                         &CLSID_DirectSoundPrivate, (void**)&dso);
224     ok(rc==E_NOINTERFACE,
225        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
226        "should have failed: %08x\n",rc);
227
228     /* try the COM class factory method of creation with a bad
229      * GUID and IID specified */
230     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
231                         &IID_IDirectSound, (void**)&dso);
232     ok(rc==REGDB_E_CLASSNOTREG,
233        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
234        "should have failed: %08x\n",rc);
235
236     /* try with no device specified */
237     rc=pDirectSoundCreate(NULL,&dso,NULL);
238     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
239        "DirectSoundCreate(NULL) failed: %08x\n",rc);
240     if (rc==S_OK && dso)
241         IDirectSound_test(dso, TRUE, NULL);
242
243     /* try with default playback device specified */
244     rc=pDirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
245     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
246        "DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %08x\n", rc);
247     if (rc==DS_OK && dso)
248         IDirectSound_test(dso, TRUE, NULL);
249
250     /* try with default voice playback device specified */
251     rc=pDirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
252     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
253        "DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %08x\n", rc);
254     if (rc==DS_OK && dso)
255         IDirectSound_test(dso, TRUE, NULL);
256
257     /* try with a bad device specified */
258     rc=pDirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
259     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
260        "should have failed: %08x\n",rc);
261     if (rc==DS_OK && dso)
262         IDirectSound_Release(dso);
263 }
264
265 static HRESULT test_dsound(LPGUID lpGuid)
266 {
267     HRESULT rc;
268     LPDIRECTSOUND dso=NULL;
269     int ref;
270
271     /* DSOUND: Error: Invalid interface buffer */
272     rc=pDirectSoundCreate(lpGuid,0,NULL);
273     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
274        "DSERR_INVALIDPARAM, returned: %08x\n",rc);
275
276     /* Create the DirectSound object */
277     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
278     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
279        "DirectSoundCreate() failed: %08x\n",rc);
280     if (rc!=DS_OK)
281         return rc;
282
283     /* Try the enumerated device */
284     IDirectSound_test(dso, TRUE, lpGuid);
285
286     /* Try the COM class factory method of creation with enumerated device */
287     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
288                         &IID_IDirectSound, (void**)&dso);
289     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
290     if (dso)
291         IDirectSound_test(dso, FALSE, lpGuid);
292
293     /* Create a DirectSound object */
294     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
295     ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
296     if (rc==DS_OK) {
297         LPDIRECTSOUND dso1=NULL;
298
299         /* Create a second DirectSound object */
300         rc=pDirectSoundCreate(lpGuid,&dso1,NULL);
301         ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
302         if (rc==DS_OK) {
303             /* Release the second DirectSound object */
304             ref=IDirectSound_Release(dso1);
305             ok(ref==0,"IDirectSound_Release() has %d references, should have "
306                "0\n",ref);
307             ok(dso!=dso1,"DirectSound objects should be unique: dso=%p,dso1=%p\n",dso,dso1);
308         }
309
310         /* Release the first DirectSound object */
311         ref=IDirectSound_Release(dso);
312         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
313            ref);
314         if (ref!=0)
315             return DSERR_GENERIC;
316     } else
317         return rc;
318
319     /* Create a DirectSound object */
320     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
321     ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
322     if (rc==DS_OK) {
323         LPDIRECTSOUNDBUFFER secondary;
324         DSBUFFERDESC bufdesc;
325         WAVEFORMATEX wfx;
326
327         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
328         ZeroMemory(&bufdesc, sizeof(bufdesc));
329         bufdesc.dwSize=sizeof(bufdesc);
330         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
331         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
332                                     wfx.nBlockAlign);
333         bufdesc.lpwfxFormat=&wfx;
334         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
335         ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
336            "IDirectSound_CreateSoundBuffer() failed to create a secondary "
337            "buffer %08x\n",rc);
338         if (rc==DS_OK && secondary!=NULL) {
339             LPDIRECTSOUND3DBUFFER buffer3d;
340             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
341                                            (void **)&buffer3d);
342             ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
343                "failed: %08x\n",rc);
344             if (rc==DS_OK && buffer3d!=NULL) {
345                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
346                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
347                    "should have 2\n",ref);
348             }
349             ref=IDirectSoundBuffer_AddRef(secondary);
350             ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
351                "should have 2\n",ref);
352         }
353         /* release with buffer */
354         ref=IDirectSound_Release(dso);
355         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
356            ref);
357         if (ref!=0)
358             return DSERR_GENERIC;
359     } else
360         return rc;
361
362     return DS_OK;
363 }
364
365 static HRESULT test_primary(LPGUID lpGuid)
366 {
367     HRESULT rc;
368     LPDIRECTSOUND dso=NULL;
369     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
370     DSBUFFERDESC bufdesc;
371     DSCAPS dscaps;
372     WAVEFORMATEX wfx;
373     int ref;
374
375     /* Create the DirectSound object */
376     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
377     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
378        "DirectSoundCreate() failed: %08x\n",rc);
379     if (rc!=DS_OK)
380         return rc;
381
382     /* Get the device capabilities */
383     ZeroMemory(&dscaps, sizeof(dscaps));
384     dscaps.dwSize=sizeof(dscaps);
385     rc=IDirectSound_GetCaps(dso,&dscaps);
386     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
387     if (rc!=DS_OK)
388         goto EXIT;
389
390     /* DSOUND: Error: Invalid buffer description pointer */
391     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
392     ok(rc==DSERR_INVALIDPARAM,
393        "IDirectSound_CreateSoundBuffer() should have failed: %08x\n", rc);
394
395     /* DSOUND: Error: NULL pointer is invalid */
396     /* DSOUND: Error: Invalid buffer description pointer */
397     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
398     ok(rc==DSERR_INVALIDPARAM && primary==0,
399        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
400        "dsbo=%p\n",rc,primary);
401
402     /* DSOUND: Error: Invalid size */
403     /* DSOUND: Error: Invalid buffer description */
404     primary=NULL;
405     ZeroMemory(&bufdesc, sizeof(bufdesc));
406     bufdesc.dwSize=sizeof(bufdesc)-1;
407     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
408     ok(rc==DSERR_INVALIDPARAM && primary==0,
409        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
410        "primary=%p\n",rc,primary);
411
412     /* DSOUND: Error: DSBCAPS_PRIMARYBUFFER flag with non-NULL lpwfxFormat */
413     /* DSOUND: Error: Invalid buffer description pointer */
414     primary=NULL;
415     ZeroMemory(&bufdesc, sizeof(bufdesc));
416     bufdesc.dwSize=sizeof(bufdesc);
417     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
418     bufdesc.lpwfxFormat=&wfx;
419     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
420     ok(rc==DSERR_INVALIDPARAM && primary==0,
421        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
422        "primary=%p\n",rc,primary);
423
424     /* DSOUND: Error: No DSBCAPS_PRIMARYBUFFER flag with NULL lpwfxFormat */
425     /* DSOUND: Error: Invalid buffer description pointer */
426     primary=NULL;
427     ZeroMemory(&bufdesc, sizeof(bufdesc));
428     bufdesc.dwSize=sizeof(bufdesc);
429     bufdesc.dwFlags=0;
430     bufdesc.lpwfxFormat=NULL;
431     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
432     ok(rc==DSERR_INVALIDPARAM && primary==0,
433        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
434        "primary=%p\n",rc,primary);
435
436     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
437     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
438     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
439     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
440     if (rc!=DS_OK)
441         goto EXIT;
442
443     /* Testing the primary buffer */
444     primary=NULL;
445     ZeroMemory(&bufdesc, sizeof(bufdesc));
446     bufdesc.dwSize=sizeof(bufdesc);
447     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
448     bufdesc.lpwfxFormat = &wfx;
449     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
450     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
451     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() should have "
452        "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
453     if (rc==DS_OK && primary!=NULL)
454         IDirectSoundBuffer_Release(primary);
455
456     primary=NULL;
457     ZeroMemory(&bufdesc, sizeof(bufdesc));
458     bufdesc.dwSize=sizeof(bufdesc);
459     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
460     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
461     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
462        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: %08x\n",rc);
463     if (rc==DSERR_CONTROLUNAVAIL)
464         trace("  No Primary\n");
465     else if (rc==DS_OK && primary!=NULL) {
466         LONG vol;
467
468         /* Try to create a second primary buffer */
469         /* DSOUND: Error: The primary buffer already exists.
470          * Any changes made to the buffer description will be ignored. */
471         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
472         ok(rc==DS_OK && second==primary,
473            "IDirectSound_CreateSoundBuffer() should have returned original "
474            "primary buffer: %08x\n",rc);
475         ref=IDirectSoundBuffer_Release(second);
476         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
477            "should have 1\n",ref);
478
479         /* Try to duplicate a primary buffer */
480         /* DSOUND: Error: Can't duplicate primary buffers */
481         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
482         /* rc=0x88780032 */
483         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
484            "should have failed %08x\n",rc);
485
486         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
487         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %08x\n", rc);
488
489         if (winetest_interactive) {
490             trace("Playing a 5 seconds reference tone at the current "
491                   "volume.\n");
492             if (rc==DS_OK)
493                 trace("(the current volume is %d according to DirectSound)\n",
494                       vol);
495             trace("All subsequent tones should be identical to this one.\n");
496             trace("Listen for stutter, changes in pitch, volume, etc.\n");
497         }
498         test_buffer(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
499                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0,FALSE,0);
500
501         ref=IDirectSoundBuffer_Release(primary);
502         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references\n",ref);
503
504         ref=IDirectSoundBuffer_AddRef(primary);
505         ok(ref==1,"IDirectSoundBuffer_AddRef() primary has %d references\n",ref);
506
507         ref=IDirectSoundBuffer_Release(primary);
508         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references\n",ref);
509
510         ref=IDirectSoundBuffer_Release(primary);
511         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
512            "should have 0\n",ref);
513     }
514
515     /* Set the CooperativeLevel back to normal */
516     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
517     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
518     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
519
520 EXIT:
521     ref=IDirectSound_Release(dso);
522     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
523     if (ref!=0)
524         return DSERR_GENERIC;
525
526     return rc;
527 }
528
529 /*
530  * Test the primary buffer at different formats while keeping the
531  * secondary buffer at a constant format.
532  */
533 static HRESULT test_primary_secondary(LPGUID lpGuid)
534 {
535     HRESULT rc;
536     LPDIRECTSOUND dso=NULL;
537     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
538     DSBUFFERDESC bufdesc;
539     DSCAPS dscaps;
540     WAVEFORMATEX wfx, wfx2;
541     int f,ref,tag;
542
543     /* Create the DirectSound object */
544     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
545     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
546        "DirectSoundCreate() failed: %08x\n",rc);
547     if (rc!=DS_OK)
548         return rc;
549
550     /* Get the device capabilities */
551     ZeroMemory(&dscaps, sizeof(dscaps));
552     dscaps.dwSize=sizeof(dscaps);
553     rc=IDirectSound_GetCaps(dso,&dscaps);
554     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
555     if (rc!=DS_OK)
556         goto EXIT;
557
558     /* We must call SetCooperativeLevel before creating primary buffer */
559     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
560     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
561     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
562     if (rc!=DS_OK)
563         goto EXIT;
564
565     ZeroMemory(&bufdesc, sizeof(bufdesc));
566     bufdesc.dwSize=sizeof(bufdesc);
567     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
568     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
569     ok(rc==DS_OK && primary!=NULL,
570        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
571
572     if (rc==DS_OK && primary!=NULL) {
573         for (f=0;f<NB_FORMATS;f++) {
574           for (tag=0;tag<NB_TAGS;tag++) {
575             /* if float, we only want to test 32-bit */
576             if ((format_tags[tag] == WAVE_FORMAT_IEEE_FLOAT) && (formats[f][1] != 32))
577                 continue;
578
579             /* We must call SetCooperativeLevel to be allowed to call
580              * SetFormat */
581             /* DSOUND: Setting DirectSound cooperative level to
582              * DSSCL_PRIORITY */
583             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
584             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
585             if (rc!=DS_OK)
586                 goto EXIT;
587
588             init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
589                         formats[f][2]);
590             wfx2=wfx;
591             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
592
593             if (wfx.wBitsPerSample <= 16)
594                 ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %08x\n",
595                    format_string(&wfx), rc);
596             else
597                 ok(rc==DS_OK || rc == E_INVALIDARG, "SetFormat (%s) failed: %08x\n",
598                    format_string(&wfx), rc);
599
600             /* There is no guarantee that SetFormat will actually change the
601              * format to what we asked for. It depends on what the soundcard
602              * supports. So we must re-query the format.
603              */
604             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
605             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", 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=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
623             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", 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 (fmt=%d) with a "
636                       "secondary buffer at %dx%dx%d\n",
637                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,format_tags[tag],
638                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
639             }
640             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
641             ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
642                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
643
644             if (rc==DS_OK && secondary!=NULL) {
645                 todo_wine ok(primary->lpVtbl==secondary->lpVtbl,
646                             "Primary and secondary buffers have different vtbls.\n");
647
648                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
649                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
650
651                 ref=IDirectSoundBuffer_Release(secondary);
652                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
653                    "should have 0\n",ref);
654             }
655           }
656         }
657
658         ref=IDirectSoundBuffer_Release(primary);
659         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
660            "should have 0\n",ref);
661     }
662
663     /* Set the CooperativeLevel back to normal */
664     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
665     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
666     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
667
668 EXIT:
669     ref=IDirectSound_Release(dso);
670     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
671     if (ref!=0)
672         return DSERR_GENERIC;
673
674     return rc;
675 }
676
677 static HRESULT test_secondary(LPGUID lpGuid)
678 {
679     HRESULT rc;
680     LPDIRECTSOUND dso=NULL;
681     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
682     DSBUFFERDESC bufdesc;
683     DSCAPS dscaps;
684     WAVEFORMATEX wfx, wfx1;
685     DWORD f, tag;
686     int ref;
687
688     /* Create the DirectSound object */
689     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
690     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
691        "DirectSoundCreate() failed: %08x\n",rc);
692     if (rc!=DS_OK)
693         return rc;
694
695     /* Get the device capabilities */
696     ZeroMemory(&dscaps, sizeof(dscaps));
697     dscaps.dwSize=sizeof(dscaps);
698     rc=IDirectSound_GetCaps(dso,&dscaps);
699     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
700     if (rc!=DS_OK)
701         goto EXIT;
702
703     /* We must call SetCooperativeLevel before creating primary buffer */
704     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
705     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
706     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", 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=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
714     ok(rc==DS_OK && primary!=NULL,
715        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",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: %08x\n", rc);
720         if (rc!=DS_OK)
721             goto EXIT1;
722
723         for (f=0;f<NB_FORMATS;f++) {
724           for (tag=0;tag<NB_TAGS;tag++) {
725             WAVEFORMATEXTENSIBLE wfxe;
726
727             /* if float, we only want to test 32-bit */
728             if ((format_tags[tag] == WAVE_FORMAT_IEEE_FLOAT) && (formats[f][1] != 32))
729                 continue;
730
731             init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
732                         formats[f][2]);
733             secondary=NULL;
734             ZeroMemory(&bufdesc, sizeof(bufdesc));
735             bufdesc.dwSize=sizeof(bufdesc);
736             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
737             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
738                                         wfx.nBlockAlign);
739             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
740             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
741                "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
742             if (rc==DS_OK && secondary!=NULL)
743                 IDirectSoundBuffer_Release(secondary);
744
745             secondary=NULL;
746             ZeroMemory(&bufdesc, sizeof(bufdesc));
747             bufdesc.dwSize=sizeof(bufdesc);
748             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
749             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
750                                         wfx.nBlockAlign);
751             bufdesc.lpwfxFormat=&wfx;
752             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
753             if (gotdx8 || wfx.wBitsPerSample <= 16 || wfx.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
754             {
755                 if (wfx.wBitsPerSample > 16)
756                     ok(((rc == DSERR_CONTROLUNAVAIL || rc == DSERR_INVALIDCALL || rc == DSERR_INVALIDPARAM /* 2003 */) && !secondary)
757                         || rc == DS_OK, /* driver dependent? */
758                         "IDirectSound_CreateSoundBuffer() "
759                         "should have returned (DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL) "
760                         "and NULL, returned: %08x %p\n", rc, secondary);
761                 else
762                     ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
763                         "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
764             }
765             else
766                 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
767                    wfx.wBitsPerSample, secondary, rc);
768
769             if (!gotdx8)
770             {
771                 win_skip("Not doing the WAVE_FORMAT_EXTENSIBLE tests\n");
772                 /* Apparently they succeed with bogus values,
773                  * which means that older dsound doesn't look at them
774                  */
775                 goto no_wfe;
776             }
777
778             if (secondary)
779                 IDirectSoundBuffer_Release(secondary);
780             secondary = NULL;
781
782             bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
783             wfxe.Format = wfx;
784             wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
785             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
786             wfxe.Format.cbSize = 1;
787             wfxe.Samples.wValidBitsPerSample = wfx.wBitsPerSample;
788             wfxe.dwChannelMask = (wfx.nChannels == 1 ? KSAUDIO_SPEAKER_MONO : KSAUDIO_SPEAKER_STEREO);
789
790             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
791             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL /* 2003 */) && !secondary,
792                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
793                 rc, secondary);
794             if (secondary)
795             {
796                 IDirectSoundBuffer_Release(secondary);
797                 secondary=NULL;
798             }
799
800             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
801
802             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
803             ok(((rc==DSERR_CONTROLUNAVAIL || rc==DSERR_INVALIDCALL || rc==DSERR_INVALIDPARAM)
804                 && !secondary)
805                || rc==DS_OK, /* 2003 / 2008 */
806                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
807                 rc, secondary);
808             if (secondary)
809             {
810                 IDirectSoundBuffer_Release(secondary);
811                 secondary=NULL;
812             }
813
814             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx);
815             wfxe.SubFormat = GUID_NULL;
816             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
817             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL) && !secondary,
818                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
819                 rc, secondary);
820             if (secondary)
821             {
822                 IDirectSoundBuffer_Release(secondary);
823                 secondary=NULL;
824             }
825             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
826
827             ++wfxe.Samples.wValidBitsPerSample;
828             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
829             ok(rc==DSERR_INVALIDPARAM && !secondary,
830                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
831                 rc, secondary);
832             if (secondary)
833             {
834                 IDirectSoundBuffer_Release(secondary);
835                 secondary=NULL;
836             }
837             --wfxe.Samples.wValidBitsPerSample;
838
839             wfxe.Samples.wValidBitsPerSample = 0;
840             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
841             ok(rc==DS_OK && secondary,
842                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
843                 rc, secondary);
844             if (secondary)
845             {
846                 IDirectSoundBuffer_Release(secondary);
847                 secondary=NULL;
848             }
849             wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
850
851             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
852             ok(rc==DS_OK && secondary!=NULL,
853                 "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
854
855 no_wfe:
856             if (rc==DS_OK && secondary!=NULL) {
857                 if (winetest_interactive) {
858                     trace("  Testing a secondary buffer at %dx%dx%d (fmt=%d) "
859                         "with a primary buffer at %dx%dx%d\n",
860                         wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,format_tags[tag],
861                         wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
862                 }
863                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
864                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
865
866                 ref=IDirectSoundBuffer_Release(secondary);
867                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
868                    "should have 0\n",ref);
869             }
870           }
871         }
872 EXIT1:
873         ref=IDirectSoundBuffer_Release(primary);
874         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
875            "should have 0\n",ref);
876     }
877
878     /* Set the CooperativeLevel back to normal */
879     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
880     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
881     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
882
883 EXIT:
884     ref=IDirectSound_Release(dso);
885     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
886     if (ref!=0)
887         return DSERR_GENERIC;
888
889     return rc;
890 }
891
892 static HRESULT test_block_align(LPGUID lpGuid)
893 {
894     HRESULT rc;
895     LPDIRECTSOUND dso=NULL;
896     LPDIRECTSOUNDBUFFER secondary=NULL;
897     DSBUFFERDESC bufdesc;
898     DSBCAPS dsbcaps;
899     WAVEFORMATEX wfx;
900     DWORD pos, pos2;
901     int ref;
902
903     /* Create the DirectSound object */
904     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
905     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
906        "DirectSoundCreate() failed: %08x\n",rc);
907     if (rc!=DS_OK)
908         return rc;
909
910     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
911     ZeroMemory(&bufdesc, sizeof(bufdesc));
912     bufdesc.dwSize=sizeof(bufdesc);
913     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
914     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
915     bufdesc.lpwfxFormat=&wfx;
916     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
917     ok(rc == DS_OK || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
918        "IDirectSound_CreateSoundBuffer() should have returned DS_OK, returned: %08x\n", rc);
919
920     if (rc==DS_OK && secondary!=NULL) {
921         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
922         dsbcaps.dwSize = sizeof(dsbcaps);
923         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
924         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
925            "returned: %08x\n", rc);
926         if (rc==DS_OK && wfx.nBlockAlign > 1)
927         {
928             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
929                "Buffer size not a multiple of nBlockAlign: requested %d, "
930                "got %d, should be %d\n", bufdesc.dwBufferBytes,
931                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
932
933             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 0);
934             ok(rc == DS_OK, "Could not set position to 0: %08x\n", rc);
935             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos, NULL);
936             ok(rc == DS_OK, "Could not get position: %08x\n", rc);
937             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 1);
938             ok(rc == DS_OK, "Could not set position to 1: %08x\n", rc);
939             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos2, NULL);
940             ok(rc == DS_OK, "Could not get new position: %08x\n", rc);
941             ok(pos == pos2, "Positions not the same! Old position: %d, new position: %d\n", pos, pos2);
942         }
943         ref=IDirectSoundBuffer_Release(secondary);
944         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
945            "should have 0\n",ref);
946     }
947
948     ref=IDirectSound_Release(dso);
949     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
950     if (ref!=0)
951         return DSERR_GENERIC;
952
953     return rc;
954 }
955
956 static struct fmt {
957     int bits;
958     int channels;
959 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
960
961 static HRESULT test_frequency(LPGUID lpGuid)
962 {
963     HRESULT rc;
964     LPDIRECTSOUND dso=NULL;
965     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
966     DSBUFFERDESC bufdesc;
967     DSCAPS dscaps;
968     WAVEFORMATEX wfx, wfx1;
969     DWORD f, r;
970     int ref;
971     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
972                     48000, 96000 };
973
974     /* Create the DirectSound object */
975     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
976     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
977        "DirectSoundCreate() failed: %08x\n",rc);
978     if (rc!=DS_OK)
979         return rc;
980
981     /* Get the device capabilities */
982     ZeroMemory(&dscaps, sizeof(dscaps));
983     dscaps.dwSize=sizeof(dscaps);
984     rc=IDirectSound_GetCaps(dso,&dscaps);
985     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
986     if (rc!=DS_OK)
987         goto EXIT;
988
989     /* We must call SetCooperativeLevel before creating primary buffer */
990     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
991     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
992     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
993     if (rc!=DS_OK)
994         goto EXIT;
995
996     ZeroMemory(&bufdesc, sizeof(bufdesc));
997     bufdesc.dwSize=sizeof(bufdesc);
998     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
999     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1000     ok(rc==DS_OK && primary!=NULL,
1001        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
1002
1003     if (rc==DS_OK && primary!=NULL) {
1004         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
1005         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
1006         if (rc!=DS_OK)
1007             goto EXIT1;
1008
1009         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
1010         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
1011             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
1012                         fmts[f].channels);
1013             secondary=NULL;
1014             ZeroMemory(&bufdesc, sizeof(bufdesc));
1015             bufdesc.dwSize=sizeof(bufdesc);
1016             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
1017             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
1018                                         BUFFER_LEN/1000,wfx.nBlockAlign);
1019             bufdesc.lpwfxFormat=&wfx;
1020             if (winetest_interactive) {
1021                 trace("  Testing a secondary buffer at %dx%dx%d "
1022                       "with a primary buffer at %dx%dx%d\n",
1023                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
1024                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
1025             }
1026             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
1027             ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
1028                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
1029
1030             if (rc==DS_OK && secondary!=NULL) {
1031                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
1032                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
1033
1034                 ref=IDirectSoundBuffer_Release(secondary);
1035                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1036                    "should have 0\n",ref);
1037             }
1038         }
1039         }
1040 EXIT1:
1041         ref=IDirectSoundBuffer_Release(primary);
1042         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1043            "should have 0\n",ref);
1044     }
1045
1046     /* Set the CooperativeLevel back to normal */
1047     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1048     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1049     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1050
1051 EXIT:
1052     ref=IDirectSound_Release(dso);
1053     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1054     if (ref!=0)
1055         return DSERR_GENERIC;
1056
1057     return rc;
1058 }
1059
1060 static HRESULT test_notify(LPDIRECTSOUNDBUFFER dsb,
1061                            DWORD count, LPHANDLE event,
1062                            DWORD expected)
1063 {
1064     HRESULT rc;
1065     DWORD ret;
1066
1067     rc=IDirectSoundBuffer_SetCurrentPosition(dsb,0);
1068     ok(rc==DS_OK,
1069        "IDirectSoundBuffer_SetCurrentPosition failed %08x\n",rc);
1070     if(rc!=DS_OK)
1071         return rc;
1072
1073     rc=IDirectSoundBuffer_Play(dsb,0,0,0);
1074     ok(rc==DS_OK,"IDirectSoundBuffer_Play failed %08x\n",rc);
1075     if(rc!=DS_OK)
1076         return rc;
1077
1078     rc=IDirectSoundBuffer_Stop(dsb);
1079     ok(rc==DS_OK,"IDirectSoundBuffer_Stop failed %08x\n",rc);
1080     if(rc!=DS_OK)
1081         return rc;
1082
1083     ret=WaitForMultipleObjects(count,event,FALSE,0);
1084     ok(ret==expected,"expected %d. got %d\n",expected,ret);
1085     return rc;
1086 }
1087
1088 static HRESULT test_duplicate(LPGUID lpGuid)
1089 {
1090     HRESULT rc;
1091     LPDIRECTSOUND dso=NULL;
1092     LPDIRECTSOUNDBUFFER primary=NULL;
1093     DSBUFFERDESC bufdesc;
1094     int ref;
1095
1096     /* Create the DirectSound object */
1097     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
1098     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
1099        "DirectSoundCreate() failed: %08x\n",rc);
1100     if (rc!=DS_OK)
1101         return rc;
1102
1103     /* We must call SetCooperativeLevel before creating primary buffer */
1104     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1105     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1106     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1107     if (rc!=DS_OK)
1108         goto EXIT;
1109
1110     ZeroMemory(&bufdesc, sizeof(bufdesc));
1111     bufdesc.dwSize=sizeof(bufdesc);
1112     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1113     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1114     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1115        "to create a primary buffer %08x\n",rc);
1116
1117     if (rc==DS_OK && primary!=NULL) {
1118         LPDIRECTSOUNDBUFFER original=NULL;
1119         WAVEFORMATEX wfx;
1120
1121         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
1122         ZeroMemory(&bufdesc, sizeof(bufdesc));
1123         bufdesc.dwSize=sizeof(bufdesc);
1124         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLPOSITIONNOTIFY;
1125         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec/100; /* very short buffer */
1126         bufdesc.lpwfxFormat=&wfx;
1127         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&original,NULL);
1128         ok(rc==DS_OK && original!=NULL,
1129            "IDirectSound_CreateSoundBuffer() failed to create a original "
1130            "buffer %08x\n",rc);
1131         if (rc==DS_OK && original!=NULL) {
1132             LPDIRECTSOUNDBUFFER duplicated=NULL;
1133             LPDIRECTSOUNDNOTIFY notify=NULL;
1134             HANDLE event[2];
1135             LPVOID buf=NULL;
1136             DWORD bufsize;
1137             int i;
1138
1139             /* Prepare notify events */
1140             for (i=0;i<sizeof(event)/sizeof(event[0]);i++) {
1141                 event[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
1142             }
1143
1144             /* Make silent buffer */
1145             rc=IDirectSoundBuffer_Lock(original,0,0,&buf,&bufsize,
1146                                        NULL,NULL,DSBLOCK_ENTIREBUFFER);
1147             ok(rc==DS_OK && buf!=NULL,
1148                "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n",rc);
1149             if (rc==DS_OK && buf!=NULL) {
1150                 ZeroMemory(buf,bufsize);
1151                 rc=IDirectSoundBuffer_Unlock(original,buf,bufsize,
1152                                              NULL,0);
1153                 ok(rc==DS_OK,"IDirectSoundBuffer_Unlock failed to unlock "
1154                    "%08x\n",rc);
1155             }
1156
1157             rc=IDirectSoundBuffer_QueryInterface(original,
1158                                                  &IID_IDirectSoundNotify,
1159                                                  (void**)&notify);
1160             ok(rc==DS_OK && notify!=NULL,
1161                "IDirectSoundBuffer_QueryInterface() failed to create a "
1162                "notification %08x\n",rc);
1163             if (rc==DS_OK && notify!=NULL) {
1164                 DSBPOSITIONNOTIFY dsbpn;
1165                 LPDIRECTSOUNDNOTIFY dup_notify=NULL;
1166
1167                 dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1168                 dsbpn.hEventNotify=event[0];
1169                 rc=IDirectSoundNotify_SetNotificationPositions(notify,
1170                                                                1,&dsbpn);
1171                 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1172                    "failed %08x\n",rc);
1173
1174                 rc=IDirectSound_DuplicateSoundBuffer(dso,original,&duplicated);
1175                 ok(rc==DS_OK && duplicated!=NULL,
1176                    "IDirectSound_DuplicateSoundBuffer failed %08x\n",rc);
1177
1178                 trace("testing duplicated buffer without notifications.\n");
1179                 test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1180                             event,WAIT_TIMEOUT);
1181
1182                 rc=IDirectSoundBuffer_QueryInterface(duplicated,
1183                                                      &IID_IDirectSoundNotify,
1184                                                      (void**)&dup_notify);
1185                 ok(rc==DS_OK&&dup_notify!=NULL,
1186                    "IDirectSoundBuffer_QueryInterface() failed to create a "
1187                    "notification %08x\n",rc);
1188                 if(rc==DS_OK&&dup_notify!=NULL) {
1189                     dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1190                     dsbpn.hEventNotify=event[1];
1191                     rc=IDirectSoundNotify_SetNotificationPositions(dup_notify,
1192                                                                    1,&dsbpn);
1193                     ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1194                        "failed %08x\n",rc);
1195
1196                     trace("testing duplicated buffer with a notification.\n");
1197                     test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1198                                 event,WAIT_OBJECT_0+1);
1199
1200                     ref=IDirectSoundNotify_Release(dup_notify);
1201                     ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1202                        "should have 0\n",ref);
1203                 }
1204                 ref=IDirectSoundNotify_Release(notify);
1205                 ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1206                    "should have 0\n",ref);
1207
1208                 trace("testing original buffer with a notification.\n");
1209                 test_notify(original,sizeof(event)/sizeof(event[0]),
1210                             event,WAIT_OBJECT_0);
1211
1212                 ref=IDirectSoundBuffer_Release(duplicated);
1213                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1214                    "should have 0\n",ref);
1215             }
1216             ref=IDirectSoundBuffer_Release(original);
1217             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1218                "should have 0\n",ref);
1219         }
1220         ref=IDirectSoundBuffer_Release(primary);
1221         ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1222            "should have 0\n",ref);
1223     }
1224
1225     /* Set the CooperativeLevel back to normal */
1226     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1227     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1228     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1229
1230 EXIT:
1231     ref=IDirectSound_Release(dso);
1232     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1233     if (ref!=0)
1234         return DSERR_GENERIC;
1235
1236     return rc;
1237 }
1238
1239 static HRESULT test_invalid_fmts(LPGUID lpGuid)
1240 {
1241     HRESULT rc;
1242     LPDIRECTSOUND dso=NULL;
1243     LPDIRECTSOUNDBUFFER primary=NULL;
1244     DSBUFFERDESC bufdesc;
1245
1246     /* Create the DirectSound object */
1247     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
1248     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
1249        "DirectSoundCreate() failed: %08x\n",rc);
1250     if (rc!=DS_OK)
1251         return rc;
1252
1253     /* We must call SetCooperativeLevel before creating primary buffer */
1254     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1255     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1256     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1257     if (rc!=DS_OK){
1258         IDirectSound_Release(dso);
1259         return rc;
1260     }
1261
1262     ZeroMemory(&bufdesc, sizeof(bufdesc));
1263     bufdesc.dwSize=sizeof(bufdesc);
1264     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1265     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1266     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1267        "to create a primary buffer %08x\n",rc);
1268
1269     if (rc==DS_OK && primary!=NULL) {
1270         WAVEFORMATEX wfx;
1271         WAVEFORMATEXTENSIBLE fmtex;
1272
1273         wfx.wFormatTag = WAVE_FORMAT_PCM;
1274         wfx.nChannels = 0;
1275         wfx.nSamplesPerSec = 44100;
1276         wfx.wBitsPerSample = 16;
1277         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1278         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1279         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1280         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1281
1282         wfx.nChannels = 2;
1283         wfx.nSamplesPerSec = 44100;
1284         wfx.wBitsPerSample = 0;
1285         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1286         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1287         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1288         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1289
1290         wfx.nChannels = 2;
1291         wfx.nSamplesPerSec = 44100;
1292         wfx.wBitsPerSample = 2;
1293         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1294         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1295         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1296         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1297
1298         wfx.nChannels = 2;
1299         wfx.nSamplesPerSec = 44100;
1300         wfx.wBitsPerSample = 12;
1301         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1302         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1303         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1304         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1305
1306         wfx.nChannels = 2;
1307         wfx.nSamplesPerSec = 0;
1308         wfx.wBitsPerSample = 16;
1309         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1310         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1311         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1312         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1313
1314         wfx.nChannels = 2;
1315         wfx.nSamplesPerSec = 44100;
1316         wfx.wBitsPerSample = 16;
1317         wfx.nBlockAlign = 0;
1318         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1319         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1320         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1321
1322         wfx.nChannels = 2;
1323         wfx.nSamplesPerSec = 44100;
1324         wfx.wBitsPerSample = 16;
1325         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1326         wfx.nAvgBytesPerSec = 0;
1327         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1328         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1329
1330         wfx.nChannels = 2;
1331         wfx.nSamplesPerSec = 44100;
1332         wfx.wBitsPerSample = 16;
1333         wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) - 1;
1334         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1335         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1336         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1337
1338         wfx.nChannels = 2;
1339         wfx.nSamplesPerSec = 44100;
1340         wfx.wBitsPerSample = 16;
1341         wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) + 1;
1342         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1343         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1344         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1345
1346         wfx.nChannels = 2;
1347         wfx.nSamplesPerSec = 44100;
1348         wfx.wBitsPerSample = 16;
1349         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1350         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1;
1351         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1352         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1353
1354         wfx.nChannels = 2;
1355         wfx.nSamplesPerSec = 44100;
1356         wfx.wBitsPerSample = 16;
1357         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1358         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign - 1;
1359         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1360         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1361
1362         wfx.nChannels = 2;
1363         wfx.nSamplesPerSec = 44100;
1364         wfx.wBitsPerSample = 16;
1365         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1366         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1;
1367         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1368         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1369
1370         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1371         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1372         fmtex.Format.nChannels = 2;
1373         fmtex.Format.nSamplesPerSec = 44100;
1374         fmtex.Format.wBitsPerSample = 16;
1375         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1376         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1377         fmtex.Samples.wValidBitsPerSample = 0;
1378         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1379         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1380         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1381         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1382
1383         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1384         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1385         fmtex.Format.nChannels = 2;
1386         fmtex.Format.nSamplesPerSec = 44100;
1387         fmtex.Format.wBitsPerSample = 24;
1388         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1389         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1390         fmtex.Samples.wValidBitsPerSample = 20;
1391         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1392         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1393         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1394         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1395
1396         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1397         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1398         fmtex.Format.nChannels = 2;
1399         fmtex.Format.nSamplesPerSec = 44100;
1400         fmtex.Format.wBitsPerSample = 24;
1401         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1402         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1403         fmtex.Samples.wValidBitsPerSample = 32;
1404         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1405         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1406         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1407         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1408
1409         IDirectSoundBuffer_Release(primary);
1410     }
1411
1412     IDirectSound_Release(dso);
1413
1414     return S_OK;
1415 }
1416
1417 static unsigned int number;
1418
1419 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1420                                    LPCSTR lpcstrModule, LPVOID lpContext)
1421 {
1422     HRESULT rc;
1423     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1424
1425     /* Don't test the primary device */
1426     if (!number++)
1427     {
1428         ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1429         return 1;
1430     }
1431
1432     rc = test_dsound(lpGuid);
1433     if (rc == DSERR_NODRIVER)
1434         trace("  No Driver\n");
1435     else if (rc == DSERR_ALLOCATED)
1436         trace("  Already In Use\n");
1437     else if (rc == E_FAIL)
1438         trace("  No Device\n");
1439     else {
1440         test_block_align(lpGuid);
1441         test_primary(lpGuid);
1442         test_primary_secondary(lpGuid);
1443         test_secondary(lpGuid);
1444         test_frequency(lpGuid);
1445         test_duplicate(lpGuid);
1446         test_invalid_fmts(lpGuid);
1447     }
1448
1449     return 1;
1450 }
1451
1452 static void dsound_tests(void)
1453 {
1454     HRESULT rc;
1455     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1456     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1457 }
1458
1459 static void test_hw_buffers(void)
1460 {
1461     IDirectSound *ds;
1462     IDirectSoundBuffer *primary, *primary2, **secondaries, *secondary;
1463     IDirectSoundBuffer8 *buf8;
1464     DSCAPS caps;
1465     DSBCAPS bufcaps;
1466     DSBUFFERDESC bufdesc;
1467     WAVEFORMATEX fmt;
1468     UINT i;
1469     HRESULT hr;
1470
1471     hr = pDirectSoundCreate(NULL, &ds, NULL);
1472     ok(hr == S_OK || hr == DSERR_NODRIVER || hr == DSERR_ALLOCATED || hr == E_FAIL,
1473             "DirectSoundCreate failed: %08x\n", hr);
1474     if(hr != S_OK)
1475         return;
1476
1477     caps.dwSize = sizeof(caps);
1478
1479     hr = IDirectSound_GetCaps(ds, &caps);
1480     ok(hr == S_OK, "GetCaps failed: %08x\n", hr);
1481
1482     ok(caps.dwPrimaryBuffers == 1, "Got wrong number of primary buffers: %u\n",
1483             caps.dwPrimaryBuffers);
1484
1485     /* DSBCAPS_LOC* is ignored for primary buffers */
1486     bufdesc.dwSize = sizeof(bufdesc);
1487     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCHARDWARE |
1488         DSBCAPS_PRIMARYBUFFER;
1489     bufdesc.dwBufferBytes = 0;
1490     bufdesc.dwReserved = 0;
1491     bufdesc.lpwfxFormat = NULL;
1492     bufdesc.guid3DAlgorithm = GUID_NULL;
1493
1494     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &primary, NULL);
1495     ok(hr == S_OK, "CreateSoundBuffer failed: %08x\n", hr);
1496     if(hr != S_OK){
1497         IDirectSound_Release(ds);
1498         return;
1499     }
1500
1501     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCSOFTWARE |
1502         DSBCAPS_PRIMARYBUFFER;
1503
1504     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &primary2, NULL);
1505     ok(hr == S_OK, "CreateSoundBuffer failed: %08x\n", hr);
1506     ok(primary == primary2, "Got different primary buffers: %p, %p\n", primary, primary2);
1507     if(hr == S_OK)
1508         IDirectSoundBuffer_Release(primary2);
1509
1510     buf8 = (IDirectSoundBuffer8 *)0xDEADBEEF;
1511     hr = IDirectSoundBuffer_QueryInterface(primary, &IID_IDirectSoundBuffer8,
1512             (void**)&buf8);
1513     ok(hr == E_NOINTERFACE, "QueryInterface gave wrong failure: %08x\n", hr);
1514     ok(buf8 == NULL, "Pointer didn't get set to NULL\n");
1515
1516     fmt.wFormatTag = WAVE_FORMAT_PCM;
1517     fmt.nChannels = 2;
1518     fmt.nSamplesPerSec = 48000;
1519     fmt.wBitsPerSample = 16;
1520     fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
1521     fmt.nAvgBytesPerSec = fmt.nBlockAlign * fmt.nSamplesPerSec;
1522     fmt.cbSize = 0;
1523
1524     bufdesc.lpwfxFormat = &fmt;
1525     bufdesc.dwBufferBytes = fmt.nSamplesPerSec * fmt.nBlockAlign / 10;
1526     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCHARDWARE |
1527         DSBCAPS_CTRLVOLUME;
1528
1529     secondaries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1530             sizeof(IDirectSoundBuffer *) * caps.dwMaxHwMixingAllBuffers);
1531
1532     /* try to fill all of the hw buffers */
1533     trace("dwMaxHwMixingAllBuffers: %u\n", caps.dwMaxHwMixingAllBuffers);
1534     trace("dwMaxHwMixingStaticBuffers: %u\n", caps.dwMaxHwMixingStaticBuffers);
1535     trace("dwMaxHwMixingStreamingBuffers: %u\n", caps.dwMaxHwMixingStreamingBuffers);
1536     for(i = 0; i < caps.dwMaxHwMixingAllBuffers; ++i){
1537         hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &secondaries[i], NULL);
1538         ok(hr == S_OK || hr == E_NOTIMPL || broken(hr == DSERR_CONTROLUNAVAIL),
1539                 "CreateSoundBuffer(%u) failed: %08x\n", i, hr);
1540         if(hr != S_OK)
1541             break;
1542
1543         bufcaps.dwSize = sizeof(bufcaps);
1544         hr = IDirectSoundBuffer_GetCaps(secondaries[i], &bufcaps);
1545         ok(hr == S_OK, "GetCaps failed: %08x\n", hr);
1546         ok((bufcaps.dwFlags & DSBCAPS_LOCHARDWARE) != 0,
1547                 "Buffer wasn't allocated in hardware, dwFlags: %x\n", bufcaps.dwFlags);
1548     }
1549
1550     /* see if we can create one more */
1551     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &secondary, NULL);
1552     ok((i == caps.dwMaxHwMixingAllBuffers && hr == DSERR_ALLOCATED) || /* out of hw buffers */
1553             (caps.dwMaxHwMixingAllBuffers == 0 && hr == DSERR_INVALIDCALL) || /* no hw buffers at all */
1554             hr == E_NOTIMPL || /* don't support hw buffers */
1555             broken(hr == DSERR_CONTROLUNAVAIL) || /* vmware winxp, others? */
1556             broken(hr == S_OK) /* broken driver allows more hw bufs than dscaps claims */,
1557             "CreateSoundBuffer gave wrong error: %08x\n", hr);
1558     if(hr == S_OK)
1559         IDirectSoundBuffer_Release(secondary);
1560
1561     for(i = 0; i < caps.dwMaxHwMixingAllBuffers; ++i)
1562         if(secondaries[i])
1563             IDirectSoundBuffer_Release(secondaries[i]);
1564
1565     IDirectSoundBuffer_Release(primary);
1566     IDirectSound_Release(ds);
1567 }
1568
1569 START_TEST(dsound)
1570 {
1571     HMODULE hDsound;
1572
1573     CoInitialize(NULL);
1574
1575     hDsound = LoadLibrary("dsound.dll");
1576     if (hDsound)
1577     {
1578         BOOL ret;
1579
1580         ret = FreeLibrary(hDsound);
1581         ok( ret, "FreeLibrary(1) returned %d\n", GetLastError());
1582         SetLastError(0xdeadbeef);
1583         ret = FreeLibrary(hDsound);
1584         ok( ret ||
1585             broken(!ret && GetLastError() == ERROR_MOD_NOT_FOUND), /* NT4 */
1586             "FreeLibrary(2) returned %d\n", GetLastError());
1587         ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1588     }
1589
1590     hDsound = LoadLibrary("dsound.dll");
1591     if (hDsound)
1592     {
1593
1594         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1595             "DirectSoundEnumerateA");
1596         pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1597             "DirectSoundCreate");
1598
1599         gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1600
1601         IDirectSound_tests();
1602         dsound_tests();
1603         test_hw_buffers();
1604
1605         FreeLibrary(hDsound);
1606     }
1607     else
1608         win_skip("dsound.dll not found - skipping all tests\n");
1609
1610     CoUninitialize();
1611 }