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