mshtml: Implement IHTMLStyle_put_textDecorationUnderline.
[wine] / dlls / dsound / tests / capture.c
1 /*
2  * Unit tests for capture functions
3  *
4  * Copyright (c) 2002 Francois Gouget
5  * Copyright (c) 2003 Robert Reif
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23 #include "initguid.h"
24 #include "windows.h"
25 #include "wine/test.h"
26 #include "dsound.h"
27 #include "mmreg.h"
28 #include "dsconf.h"
29
30 #include "dsound_test.h"
31
32 #define NOTIFICATIONS    5
33
34 static HRESULT (WINAPI *pDirectSoundCaptureCreate)(LPCGUID,LPDIRECTSOUNDCAPTURE*,LPUNKNOWN)=NULL;
35 static HRESULT (WINAPI *pDirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
36
37 static const char * get_format_str(WORD format)
38 {
39     static char msg[32];
40 #define WAVE_FORMAT(f) case f: return #f
41     switch (format) {
42     WAVE_FORMAT(WAVE_FORMAT_PCM);
43     WAVE_FORMAT(WAVE_FORMAT_ADPCM);
44     WAVE_FORMAT(WAVE_FORMAT_IBM_CVSD);
45     WAVE_FORMAT(WAVE_FORMAT_ALAW);
46     WAVE_FORMAT(WAVE_FORMAT_MULAW);
47     WAVE_FORMAT(WAVE_FORMAT_OKI_ADPCM);
48     WAVE_FORMAT(WAVE_FORMAT_IMA_ADPCM);
49     WAVE_FORMAT(WAVE_FORMAT_MEDIASPACE_ADPCM);
50     WAVE_FORMAT(WAVE_FORMAT_SIERRA_ADPCM);
51     WAVE_FORMAT(WAVE_FORMAT_G723_ADPCM);
52     WAVE_FORMAT(WAVE_FORMAT_DIGISTD);
53     WAVE_FORMAT(WAVE_FORMAT_DIGIFIX);
54     WAVE_FORMAT(WAVE_FORMAT_DIALOGIC_OKI_ADPCM);
55     WAVE_FORMAT(WAVE_FORMAT_YAMAHA_ADPCM);
56     WAVE_FORMAT(WAVE_FORMAT_SONARC);
57     WAVE_FORMAT(WAVE_FORMAT_DSPGROUP_TRUESPEECH);
58     WAVE_FORMAT(WAVE_FORMAT_ECHOSC1);
59     WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF36);
60     WAVE_FORMAT(WAVE_FORMAT_APTX);
61     WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF10);
62     WAVE_FORMAT(WAVE_FORMAT_DOLBY_AC2);
63     WAVE_FORMAT(WAVE_FORMAT_GSM610);
64     WAVE_FORMAT(WAVE_FORMAT_ANTEX_ADPCME);
65     WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_VQLPC);
66     WAVE_FORMAT(WAVE_FORMAT_DIGIREAL);
67     WAVE_FORMAT(WAVE_FORMAT_DIGIADPCM);
68     WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_CR10);
69     WAVE_FORMAT(WAVE_FORMAT_NMS_VBXADPCM);
70     WAVE_FORMAT(WAVE_FORMAT_G721_ADPCM);
71     WAVE_FORMAT(WAVE_FORMAT_MPEG);
72     WAVE_FORMAT(WAVE_FORMAT_MPEGLAYER3);
73     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_ADPCM);
74     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH8);
75     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH10);
76     WAVE_FORMAT(WAVE_FORMAT_FM_TOWNS_SND);
77     WAVE_FORMAT(WAVE_FORMAT_OLIGSM);
78     WAVE_FORMAT(WAVE_FORMAT_OLIADPCM);
79     WAVE_FORMAT(WAVE_FORMAT_OLICELP);
80     WAVE_FORMAT(WAVE_FORMAT_OLISBC);
81     WAVE_FORMAT(WAVE_FORMAT_OLIOPR);
82     WAVE_FORMAT(WAVE_FORMAT_DEVELOPMENT);
83     WAVE_FORMAT(WAVE_FORMAT_EXTENSIBLE);
84     }
85 #undef WAVE_FORMAT
86     sprintf(msg, "Unknown(0x%04x)", format);
87     return msg;
88 }
89
90 const char * format_string(const WAVEFORMATEX* wfx)
91 {
92     static char str[64];
93
94     sprintf(str, "%5dx%2dx%d %s",
95         wfx->nSamplesPerSec, wfx->wBitsPerSample, wfx->nChannels,
96         get_format_str(wfx->wFormatTag));
97
98     return str;
99 }
100
101 static void IDirectSoundCapture_test(LPDIRECTSOUNDCAPTURE dsco,
102                                      BOOL initialized, LPCGUID lpGuid)
103 {
104     HRESULT rc;
105     DSCCAPS dsccaps;
106     int ref;
107     IUnknown * unknown;
108     IDirectSoundCapture * dsc;
109
110     /* Try to Query for objects */
111     rc=IDirectSoundCapture_QueryInterface(dsco, &IID_IUnknown,
112                                           (LPVOID*)&unknown);
113     ok(rc==DS_OK, "IDirectSoundCapture_QueryInterface(IID_IUnknown) "
114        "failed: %08x\n", rc);
115     if (rc==DS_OK)
116         IDirectSoundCapture_Release(unknown);
117
118     rc=IDirectSoundCapture_QueryInterface(dsco, &IID_IDirectSoundCapture,
119                                           (LPVOID*)&dsc);
120     ok(rc==DS_OK, "IDirectSoundCapture_QueryInterface(IID_IDirectSoundCapture) "
121        "failed: %08x\n", rc);
122     if (rc==DS_OK)
123         IDirectSoundCapture_Release(dsc);
124
125     if (initialized == FALSE) {
126         /* try uninitialized object */
127         rc=IDirectSoundCapture_GetCaps(dsco,0);
128         ok(rc==DSERR_UNINITIALIZED||rc==E_INVALIDARG,
129            "IDirectSoundCapture_GetCaps(NULL) should have returned "
130            "DSERR_UNINITIALIZED or E_INVALIDARG, returned: %08x\n", rc);
131
132         rc=IDirectSoundCapture_GetCaps(dsco, &dsccaps);
133         ok(rc==DSERR_UNINITIALIZED,"IDirectSoundCapture_GetCaps() "
134            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
135
136         rc=IDirectSoundCapture_Initialize(dsco, lpGuid);
137         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||
138            rc==E_FAIL||rc==E_INVALIDARG,
139            "IDirectSoundCapture_Initialize() failed: %08x\n", rc);
140         if (rc==DSERR_NODRIVER||rc==E_INVALIDARG) {
141             trace("  No Driver\n");
142             goto EXIT;
143         } else if (rc==E_FAIL) {
144             trace("  No Device\n");
145             goto EXIT;
146         } else if (rc==DSERR_ALLOCATED) {
147             trace("  Already In Use\n");
148             goto EXIT;
149         }
150     }
151
152     rc=IDirectSoundCapture_Initialize(dsco, lpGuid);
153     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSoundCapture_Initialize() "
154        "should have returned DSERR_ALREADYINITIALIZED: %08x\n", rc);
155
156     /* DSOUND: Error: Invalid caps buffer */
157     rc=IDirectSoundCapture_GetCaps(dsco, 0);
158     ok(rc==DSERR_INVALIDPARAM, "IDirectSoundCapture_GetCaps(NULL) "
159        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
160
161     ZeroMemory(&dsccaps, sizeof(dsccaps));
162
163     /* DSOUND: Error: Invalid caps buffer */
164     rc=IDirectSound_GetCaps(dsco, &dsccaps);
165     ok(rc==DSERR_INVALIDPARAM, "IDirectSound_GetCaps() "
166        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
167
168     dsccaps.dwSize=sizeof(dsccaps);
169
170     /* DSOUND: Running on a certified driver */
171     rc=IDirectSoundCapture_GetCaps(dsco, &dsccaps);
172     ok(rc==DS_OK, "IDirectSoundCapture_GetCaps() failed: %08x\n", rc);
173
174 EXIT:
175     ref=IDirectSoundCapture_Release(dsco);
176     ok(ref==0, "IDirectSoundCapture_Release() has %d references, "
177        "should have 0\n", ref);
178 }
179
180 static void IDirectSoundCapture_tests(void)
181 {
182     HRESULT rc;
183     LPDIRECTSOUNDCAPTURE dsco=NULL;
184     LPCLASSFACTORY cf=NULL;
185
186     trace("Testing IDirectSoundCapture\n");
187
188     rc=CoGetClassObject(&CLSID_DirectSoundCapture, CLSCTX_INPROC_SERVER, NULL,
189                         &IID_IClassFactory, (void**)&cf);
190     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSoundCapture, IID_IClassFactory) "
191        "failed: %08x\n", rc);
192
193     rc=CoGetClassObject(&CLSID_DirectSoundCapture, CLSCTX_INPROC_SERVER, NULL,
194                         &IID_IUnknown, (void**)&cf);
195     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSoundCapture, IID_IUnknown) "
196        "failed: %08x\n", rc);
197
198     /* try the COM class factory method of creation with no device specified */
199     rc=CoCreateInstance(&CLSID_DirectSoundCapture, NULL, CLSCTX_INPROC_SERVER,
200                         &IID_IDirectSoundCapture, (void**)&dsco);
201     ok(rc==S_OK||rc==REGDB_E_CLASSNOTREG,"CoCreateInstance(CLSID_DirectSoundCapture) failed: %08x\n", rc);
202     if (rc==REGDB_E_CLASSNOTREG) {
203         trace("  Class Not Registered\n");
204         return;
205     }
206     if (dsco)
207         IDirectSoundCapture_test(dsco, FALSE, NULL);
208
209     /* try the COM class factory method of creation with default capture
210      * device specified */
211     rc=CoCreateInstance(&CLSID_DirectSoundCapture, NULL, CLSCTX_INPROC_SERVER,
212                         &IID_IDirectSoundCapture, (void**)&dsco);
213     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSoundCapture) failed: %08x\n", rc);
214     if (dsco)
215         IDirectSoundCapture_test(dsco, FALSE, &DSDEVID_DefaultCapture);
216
217     /* try the COM class factory method of creation with default voice
218      * capture device specified */
219     rc=CoCreateInstance(&CLSID_DirectSoundCapture, NULL, CLSCTX_INPROC_SERVER,
220                         &IID_IDirectSoundCapture, (void**)&dsco);
221     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSoundCapture) failed: %08x\n", rc);
222     if (dsco)
223         IDirectSoundCapture_test(dsco, FALSE, &DSDEVID_DefaultVoiceCapture);
224
225     /* try the COM class factory method of creation with a bad
226      * IID specified */
227     rc=CoCreateInstance(&CLSID_DirectSoundCapture, NULL, CLSCTX_INPROC_SERVER,
228                         &CLSID_DirectSoundPrivate, (void**)&dsco);
229     ok(rc==E_NOINTERFACE,
230        "CoCreateInstance(CLSID_DirectSoundCapture,CLSID_DirectSoundPrivate) "
231        "should have failed: %08x\n",rc);
232
233     /* try with no device specified */
234     rc=pDirectSoundCaptureCreate(NULL,&dsco,NULL);
235     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
236        "DirectSoundCaptureCreate(NULL) failed: %08x\n",rc);
237     if (rc==S_OK && dsco)
238         IDirectSoundCapture_test(dsco, TRUE, NULL);
239
240     /* try with default capture device specified */
241     rc=pDirectSoundCaptureCreate(&DSDEVID_DefaultCapture,&dsco,NULL);
242     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
243        "DirectSoundCaptureCreate(DSDEVID_DefaultCapture) failed: %08x\n", rc);
244     if (rc==DS_OK && dsco)
245         IDirectSoundCapture_test(dsco, TRUE, NULL);
246
247     /* try with default voice capture device specified */
248     rc=pDirectSoundCaptureCreate(&DSDEVID_DefaultVoiceCapture,&dsco,NULL);
249     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
250        "DirectSoundCaptureCreate(DSDEVID_DefaultVoiceCapture) failed: %08x\n", rc);
251     if (rc==DS_OK && dsco)
252         IDirectSoundCapture_test(dsco, TRUE, NULL);
253
254     /* try with a bad device specified */
255     rc=pDirectSoundCaptureCreate(&DSDEVID_DefaultVoicePlayback,&dsco,NULL);
256     ok(rc==DSERR_NODRIVER,
257        "DirectSoundCaptureCreate(DSDEVID_DefaultVoicePlatback) "
258        "should have failed: %08x\n",rc);
259     if (rc==DS_OK && dsco)
260         IDirectSoundCapture_Release(dsco);
261 }
262
263 typedef struct {
264     char* wave;
265     DWORD wave_len;
266
267     LPDIRECTSOUNDCAPTUREBUFFER dscbo;
268     LPWAVEFORMATEX wfx;
269     DSBPOSITIONNOTIFY posnotify[NOTIFICATIONS];
270     HANDLE event[NOTIFICATIONS];
271     LPDIRECTSOUNDNOTIFY notify;
272
273     DWORD buffer_size;
274     DWORD read;
275     DWORD offset;
276     DWORD size;
277
278     DWORD last_pos;
279 } capture_state_t;
280
281 static int capture_buffer_service(capture_state_t* state)
282 {
283     HRESULT rc;
284     LPVOID ptr1,ptr2;
285     DWORD len1,len2;
286     DWORD capture_pos,read_pos;
287
288     rc=IDirectSoundCaptureBuffer_GetCurrentPosition(state->dscbo,&capture_pos,
289                                                     &read_pos);
290     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetCurrentPosition() failed: %08x\n", rc);
291     if (rc!=DS_OK)
292         return 0;
293
294     rc=IDirectSoundCaptureBuffer_Lock(state->dscbo,state->offset,state->size,
295                                       &ptr1,&len1,&ptr2,&len2,0);
296     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Lock() failed: %08x\n", rc);
297     if (rc!=DS_OK)
298         return 0;
299
300     rc=IDirectSoundCaptureBuffer_Unlock(state->dscbo,ptr1,len1,ptr2,len2);
301     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Unlock() failed: %08x\n", rc);
302     if (rc!=DS_OK)
303         return 0;
304
305     state->offset = (state->offset + state->size) % state->buffer_size;
306
307     return 1;
308 }
309
310 static void test_capture_buffer(LPDIRECTSOUNDCAPTURE dsco,
311                                 LPDIRECTSOUNDCAPTUREBUFFER dscbo, int record)
312 {
313     HRESULT rc;
314     DSCBCAPS dscbcaps;
315     WAVEFORMATEX wfx;
316     DWORD size,status;
317     capture_state_t state;
318     int i, ref;
319
320     /* Private dsound.dll: Error: Invalid caps pointer */
321     rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,0);
322     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetCaps() should "
323        "have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
324
325     /* Private dsound.dll: Error: Invalid caps pointer */
326     dscbcaps.dwSize=0;
327     rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
328     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetCaps() should "
329        "have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
330
331     dscbcaps.dwSize=sizeof(dscbcaps);
332     rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
333     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetCaps() failed: %08x\n", rc);
334     if (rc==DS_OK && winetest_debug > 1) {
335         trace("    Caps: size = %d flags=0x%08x buffer size=%d\n",
336             dscbcaps.dwSize,dscbcaps.dwFlags,dscbcaps.dwBufferBytes);
337     }
338
339     /* Query the format size. Note that it may not match sizeof(wfx) */
340     /* Private dsound.dll: Error: Either pwfxFormat or pdwSizeWritten must
341      * be non-NULL */
342     rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,NULL);
343     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetFormat() should "
344        "have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
345
346     size=0;
347     rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,&size);
348     ok(rc==DS_OK && size!=0,"IDirectSoundCaptureBuffer_GetFormat() should "
349        "have returned the needed size: rc=%08x, size=%d\n", rc,size);
350
351     rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,&wfx,sizeof(wfx),NULL);
352     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetFormat() failed: %08x\n", rc);
353     if (rc==DS_OK && winetest_debug > 1) {
354         trace("    Format: tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
355               wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
356               wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
357     }
358
359     /* Private dsound.dll: Error: Invalid status pointer */
360     rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,0);
361     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetStatus() should "
362        "have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
363
364     rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
365     ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetStatus() failed: %08x\n", rc);
366     if (rc==DS_OK && winetest_debug > 1) {
367         trace("    Status=0x%04x\n",status);
368     }
369
370     ZeroMemory(&state, sizeof(state));
371     state.dscbo=dscbo;
372     state.wfx=&wfx;
373     state.buffer_size = dscbcaps.dwBufferBytes;
374     for (i = 0; i < NOTIFICATIONS; i++)
375         state.event[i] = CreateEvent( NULL, FALSE, FALSE, NULL );
376     state.size = dscbcaps.dwBufferBytes / NOTIFICATIONS;
377
378     rc=IDirectSoundCaptureBuffer_QueryInterface(dscbo,&IID_IDirectSoundNotify,
379                                                 (void **)&(state.notify));
380     ok((rc==DS_OK)&&(state.notify!=NULL),
381        "IDirectSoundCaptureBuffer_QueryInterface() failed: %08x\n", rc);
382     if (rc!=DS_OK)
383         return;
384
385     for (i = 0; i < NOTIFICATIONS; i++) {
386         state.posnotify[i].dwOffset = (i * state.size) + state.size - 1;
387         state.posnotify[i].hEventNotify = state.event[i];
388     }
389
390     rc=IDirectSoundNotify_SetNotificationPositions(state.notify,NOTIFICATIONS,
391                                                    state.posnotify);
392     ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions() failed: %08x\n", rc);
393     if (rc!=DS_OK)
394         return;
395
396     ref=IDirectSoundNotify_Release(state.notify);
397     ok(ref==0,"IDirectSoundNotify_Release(): has %d references, should have "
398        "0\n",ref);
399     if (ref!=0)
400         return;
401
402     if (record) {
403         rc=IDirectSoundCaptureBuffer_Start(dscbo,DSCBSTART_LOOPING);
404         ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Start() failed: %08x\n", rc);
405         if (rc!=DS_OK)
406             return;
407
408         rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
409         ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetStatus() failed: %08x\n", rc);
410         ok(status==(DSCBSTATUS_CAPTURING|DSCBSTATUS_LOOPING),
411            "GetStatus: bad status: %x\n",status);
412         if (rc!=DS_OK)
413             return;
414
415         /* wait for the notifications */
416         for (i = 0; i < (NOTIFICATIONS * 2); i++) {
417             rc=WaitForMultipleObjects(NOTIFICATIONS,state.event,FALSE,3000);
418             ok(rc==(WAIT_OBJECT_0+(i%NOTIFICATIONS)),
419                "WaitForMultipleObjects failed: 0x%x\n",rc);
420             if (rc!=(WAIT_OBJECT_0+(i%NOTIFICATIONS))) {
421                 ok((rc==WAIT_TIMEOUT)||(rc==WAIT_FAILED),
422                    "Wrong notification: should be %d, got %d\n",
423                     i%NOTIFICATIONS,rc-WAIT_OBJECT_0);
424             }
425             if (!capture_buffer_service(&state))
426                 break;
427         }
428
429         rc=IDirectSoundCaptureBuffer_Stop(dscbo);
430         ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Stop() failed: %08x\n", rc);
431         if (rc!=DS_OK)
432             return;
433     }
434 }
435
436 static BOOL WINAPI dscenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
437                                     LPCSTR lpcstrModule, LPVOID lpContext)
438 {
439     HRESULT rc;
440     LPDIRECTSOUNDCAPTURE dsco=NULL;
441     LPDIRECTSOUNDCAPTUREBUFFER dscbo=NULL;
442     DSCBUFFERDESC bufdesc;
443     WAVEFORMATEX wfx;
444     DSCCAPS dsccaps;
445     DWORD f;
446     int ref;
447
448     /* Private dsound.dll: Error: Invalid interface buffer */
449     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
450     rc=pDirectSoundCaptureCreate(lpGuid,NULL,NULL);
451     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCaptureCreate() should have "
452        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
453
454     rc=pDirectSoundCaptureCreate(lpGuid,&dsco,NULL);
455     ok((rc==DS_OK)||(rc==DSERR_NODRIVER)||(rc==E_FAIL)||(rc==DSERR_ALLOCATED),
456        "DirectSoundCaptureCreate() failed: %08x\n",rc);
457     if (rc!=DS_OK) {
458         if (rc==DSERR_NODRIVER)
459             trace("  No Driver\n");
460         else if (rc==E_FAIL)
461             trace("  No Device\n");
462         else if (rc==DSERR_ALLOCATED)
463             trace("  Already In Use\n");
464         goto EXIT;
465     }
466
467     /* Private dsound.dll: Error: Invalid caps buffer */
468     rc=IDirectSoundCapture_GetCaps(dsco,NULL);
469     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_GetCaps() should have "
470        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
471
472     /* Private dsound.dll: Error: Invalid caps buffer */
473     dsccaps.dwSize=0;
474     rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
475     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_GetCaps() should have "
476        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
477
478     dsccaps.dwSize=sizeof(dsccaps);
479     rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
480     ok(rc==DS_OK,"IDirectSoundCapture_GetCaps() failed: %08x\n", rc);
481     if (rc==DS_OK && winetest_debug > 1) {
482         trace("  Caps: size=%d flags=0x%08x formats=%05x channels=%d\n",
483               dsccaps.dwSize,dsccaps.dwFlags,dsccaps.dwFormats,
484               dsccaps.dwChannels);
485     }
486
487     /* Private dsound.dll: Error: Invalid size */
488     /* Private dsound.dll: Error: Invalid capture buffer description */
489     ZeroMemory(&bufdesc, sizeof(bufdesc));
490     bufdesc.dwSize=0;
491     bufdesc.dwFlags=0;
492     bufdesc.dwBufferBytes=0;
493     bufdesc.dwReserved=0;
494     bufdesc.lpwfxFormat=NULL;
495     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
496     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
497        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
498     if (rc==DS_OK) {
499         ref=IDirectSoundCaptureBuffer_Release(dscbo);
500         ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
501            "should have 0\n",ref);
502     }
503
504     /* Private dsound.dll: Error: Invalid buffer size */
505     /* Private dsound.dll: Error: Invalid capture buffer description */
506     ZeroMemory(&bufdesc, sizeof(bufdesc));
507     bufdesc.dwSize=sizeof(bufdesc);
508     bufdesc.dwFlags=0;
509     bufdesc.dwBufferBytes=0;
510     bufdesc.dwReserved=0;
511     bufdesc.lpwfxFormat=NULL;
512     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
513     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
514        "should have returned DSERR_INVALIDPARAM, returned %08x\n", rc);
515     if (rc==DS_OK) {
516         ref=IDirectSoundCaptureBuffer_Release(dscbo);
517         ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
518            "should have 0\n",ref);
519     }
520
521     /* Private dsound.dll: Error: Invalid buffer size */
522     /* Private dsound.dll: Error: Invalid capture buffer description */
523     ZeroMemory(&bufdesc, sizeof(bufdesc));
524     ZeroMemory(&wfx, sizeof(wfx));
525     bufdesc.dwSize=sizeof(bufdesc);
526     bufdesc.dwFlags=0;
527     bufdesc.dwBufferBytes=0;
528     bufdesc.dwReserved=0;
529     bufdesc.lpwfxFormat=&wfx;
530     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
531     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
532        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
533     if (rc==DS_OK) {
534         ref=IDirectSoundCaptureBuffer_Release(dscbo);
535         ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
536            "should have 0\n",ref);
537     }
538
539     /* Private dsound.dll: Error: Invalid buffer size */
540     /* Private dsound.dll: Error: Invalid capture buffer description */
541     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
542     ZeroMemory(&bufdesc, sizeof(bufdesc));
543     bufdesc.dwSize=sizeof(bufdesc);
544     bufdesc.dwFlags=0;
545     bufdesc.dwBufferBytes=0;
546     bufdesc.dwReserved=0;
547     bufdesc.lpwfxFormat=&wfx;
548     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
549     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
550        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
551     if (rc==DS_OK) {
552         ref=IDirectSoundCaptureBuffer_Release(dscbo);
553         ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
554            "should have 0\n",ref);
555     }
556
557     for (f=0;f<NB_FORMATS;f++) {
558         dscbo=NULL;
559         init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
560                     formats[f][2]);
561         ZeroMemory(&bufdesc, sizeof(bufdesc));
562         bufdesc.dwSize=sizeof(bufdesc);
563         bufdesc.dwFlags=0;
564         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
565         bufdesc.dwReserved=0;
566         bufdesc.lpwfxFormat=&wfx;
567         if (winetest_interactive)
568             trace("  Testing the capture buffer at %s\n", format_string(&wfx));
569         rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
570         ok(((rc==DS_OK)&&(dscbo!=NULL))
571            || rc==DSERR_BADFORMAT || rc==DSERR_INVALIDCALL || rc==DSERR_NODRIVER
572            || rc==DSERR_ALLOCATED || rc==E_INVALIDARG || rc==E_FAIL,
573            "IDirectSoundCapture_CreateCaptureBuffer() failed to create a "
574            "%s capture buffer: %08x\n",format_string(&wfx),rc);
575         if (rc==DS_OK) {
576             test_capture_buffer(dsco, dscbo, winetest_interactive);
577             ref=IDirectSoundCaptureBuffer_Release(dscbo);
578             ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
579                "should have 0\n",ref);
580         } else if (rc==DSERR_BADFORMAT) {
581             ok(!(dsccaps.dwFormats & formats[f][3]),
582                "IDirectSoundCapture_CreateCaptureBuffer() failed to create a "
583                "capture buffer: format listed as supported but using it failed\n");
584             if (!(dsccaps.dwFormats & formats[f][3]))
585                 trace("  Format not supported: %s\n", format_string(&wfx));
586         } else if (rc==DSERR_NODRIVER) {
587             trace("  No Driver\n");
588         } else if (rc==DSERR_ALLOCATED) {
589             trace("  Already In Use\n");
590         } else if (rc==E_INVALIDARG) { /* try the old version struct */
591             DSCBUFFERDESC1 bufdesc1;
592             ZeroMemory(&bufdesc1, sizeof(bufdesc1));
593             bufdesc1.dwSize=sizeof(bufdesc1);
594             bufdesc1.dwFlags=0;
595             bufdesc1.dwBufferBytes=wfx.nAvgBytesPerSec;
596             bufdesc1.dwReserved=0;
597             bufdesc1.lpwfxFormat=&wfx;
598             rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,
599                 (DSCBUFFERDESC*)&bufdesc1,&dscbo,NULL);
600             ok(rc==DS_OK || broken(rc==DSERR_INVALIDPARAM),
601                "IDirectSoundCapture_CreateCaptureBuffer() failed to create a "
602                "%s capture buffer: %08x\n",format_string(&wfx), rc);
603             if (rc==DSERR_INVALIDPARAM) {
604                 skip("broken driver\n");
605                 goto EXIT;
606             }
607             if (rc==DS_OK) {
608                 test_capture_buffer(dsco, dscbo, winetest_interactive);
609                 ref=IDirectSoundCaptureBuffer_Release(dscbo);
610                 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d "
611                    "references, should have 0\n",ref);
612             }
613         } else if (rc==E_FAIL) {
614             /* WAVE_FORMAT_PCM only allows 8 and 16 bits per sample, so only
615              * report a failure if the bits per sample is 8 or 16
616              */
617             if (wfx.wBitsPerSample == 8 || wfx.wBitsPerSample == 16)
618                 ok(FALSE,"Should not fail for 8 or 16 bits per sample\n");
619         }
620     }
621
622     /* try a non PCM format */
623     if (0)
624     {
625     /* FIXME: Why is this commented out? */
626     init_format(&wfx,WAVE_FORMAT_MULAW,8000,8,1);
627     ZeroMemory(&bufdesc, sizeof(bufdesc));
628     bufdesc.dwSize=sizeof(bufdesc);
629     bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
630     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
631     bufdesc.dwReserved=0;
632     bufdesc.lpwfxFormat=&wfx;
633     if (winetest_interactive)
634         trace("  Testing the capture buffer at %s\n", format_string(&wfx));
635     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
636     ok((rc==DS_OK)&&(dscbo!=NULL),"IDirectSoundCapture_CreateCaptureBuffer() "
637        "failed to create a capture buffer: %08x\n",rc);
638     if ((rc==DS_OK)&&(dscbo!=NULL)) {
639         test_capture_buffer(dsco, dscbo, winetest_interactive);
640         ref=IDirectSoundCaptureBuffer_Release(dscbo);
641         ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
642            "should have 0\n",ref);
643     }
644     }
645
646     /* Try an invalid format to test error handling */
647     if (0)
648     {
649     /* FIXME: Remove this test altogether? */
650     init_format(&wfx,WAVE_FORMAT_PCM,2000000,16,2);
651     ZeroMemory(&bufdesc, sizeof(bufdesc));
652     bufdesc.dwSize=sizeof(bufdesc);
653     bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
654     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
655     bufdesc.dwReserved=0;
656     bufdesc.lpwfxFormat=&wfx;
657     if (winetest_interactive)
658         trace("  Testing the capture buffer at %s\n", format_string(&wfx));
659     rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
660     ok(rc!=DS_OK,"IDirectSoundCapture_CreateCaptureBuffer() should have failed "
661        "at 2 MHz %08x\n",rc);
662     }
663
664 EXIT:
665     if (dsco!=NULL) {
666         ref=IDirectSoundCapture_Release(dsco);
667         ok(ref==0,"IDirectSoundCapture_Release() has %d references, should "
668            "have 0\n",ref);
669     }
670
671     return TRUE;
672 }
673
674 static void capture_tests(void)
675 {
676     HRESULT rc;
677     rc=pDirectSoundCaptureEnumerateA(&dscenum_callback,NULL);
678     ok(rc==DS_OK,"DirectSoundCaptureEnumerateA() failed: %08x\n", rc);
679 }
680
681 START_TEST(capture)
682 {
683     HMODULE hDsound;
684
685     CoInitialize(NULL);
686
687     hDsound = LoadLibrary("dsound.dll");
688     if (hDsound)
689     {
690         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
691
692         pDirectSoundCaptureCreate=(void*)GetProcAddress(hDsound,
693             "DirectSoundCaptureCreate");
694         pDirectSoundCaptureEnumerateA=(void*)GetProcAddress(hDsound,
695             "DirectSoundCaptureEnumerateA");
696         if (pDirectSoundCaptureCreate && pDirectSoundCaptureEnumerateA)
697         {
698             IDirectSoundCapture_tests();
699             capture_tests();
700         }
701         else
702             skip("capture test skipped\n");
703
704         FreeLibrary(hDsound);
705     }
706     else
707         skip("dsound.dll not found!\n");
708
709     CoUninitialize();
710 }