2 * Unit tests for capture functions
4 * Copyright (c) 2002 Francois Gouget
5 * Copyright (c) 2003 Robert Reif
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define NONAMELESSSTRUCT
23 #define NONAMELESSUNION
28 #include "wine/test.h"
33 #include "dsound_test.h"
35 #define NOTIFICATIONS 5
37 static HRESULT (WINAPI *pDirectSoundCaptureCreate)(LPCGUID,LPDIRECTSOUNDCAPTURE*,LPUNKNOWN)=NULL;
38 static HRESULT (WINAPI *pDirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
40 static const char * get_format_str(WORD format)
43 #define WAVE_FORMAT(f) case f: return #f
45 WAVE_FORMAT(WAVE_FORMAT_PCM);
46 WAVE_FORMAT(WAVE_FORMAT_ADPCM);
47 WAVE_FORMAT(WAVE_FORMAT_IBM_CVSD);
48 WAVE_FORMAT(WAVE_FORMAT_ALAW);
49 WAVE_FORMAT(WAVE_FORMAT_MULAW);
50 WAVE_FORMAT(WAVE_FORMAT_OKI_ADPCM);
51 WAVE_FORMAT(WAVE_FORMAT_IMA_ADPCM);
52 WAVE_FORMAT(WAVE_FORMAT_MEDIASPACE_ADPCM);
53 WAVE_FORMAT(WAVE_FORMAT_SIERRA_ADPCM);
54 WAVE_FORMAT(WAVE_FORMAT_G723_ADPCM);
55 WAVE_FORMAT(WAVE_FORMAT_DIGISTD);
56 WAVE_FORMAT(WAVE_FORMAT_DIGIFIX);
57 WAVE_FORMAT(WAVE_FORMAT_DIALOGIC_OKI_ADPCM);
58 WAVE_FORMAT(WAVE_FORMAT_YAMAHA_ADPCM);
59 WAVE_FORMAT(WAVE_FORMAT_SONARC);
60 WAVE_FORMAT(WAVE_FORMAT_DSPGROUP_TRUESPEECH);
61 WAVE_FORMAT(WAVE_FORMAT_ECHOSC1);
62 WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF36);
63 WAVE_FORMAT(WAVE_FORMAT_APTX);
64 WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF10);
65 WAVE_FORMAT(WAVE_FORMAT_DOLBY_AC2);
66 WAVE_FORMAT(WAVE_FORMAT_GSM610);
67 WAVE_FORMAT(WAVE_FORMAT_ANTEX_ADPCME);
68 WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_VQLPC);
69 WAVE_FORMAT(WAVE_FORMAT_DIGIREAL);
70 WAVE_FORMAT(WAVE_FORMAT_DIGIADPCM);
71 WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_CR10);
72 WAVE_FORMAT(WAVE_FORMAT_NMS_VBXADPCM);
73 WAVE_FORMAT(WAVE_FORMAT_G721_ADPCM);
74 WAVE_FORMAT(WAVE_FORMAT_MPEG);
75 WAVE_FORMAT(WAVE_FORMAT_MPEGLAYER3);
76 WAVE_FORMAT(WAVE_FORMAT_CREATIVE_ADPCM);
77 WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH8);
78 WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH10);
79 WAVE_FORMAT(WAVE_FORMAT_FM_TOWNS_SND);
80 WAVE_FORMAT(WAVE_FORMAT_OLIGSM);
81 WAVE_FORMAT(WAVE_FORMAT_OLIADPCM);
82 WAVE_FORMAT(WAVE_FORMAT_OLICELP);
83 WAVE_FORMAT(WAVE_FORMAT_OLISBC);
84 WAVE_FORMAT(WAVE_FORMAT_OLIOPR);
85 WAVE_FORMAT(WAVE_FORMAT_DEVELOPMENT);
86 WAVE_FORMAT(WAVE_FORMAT_EXTENSIBLE);
89 sprintf(msg, "Unknown(0x%04x)", format);
93 static char * format_string(WAVEFORMATEX* wfx)
97 sprintf(str, "%5ldx%2dx%d %s",
98 wfx->nSamplesPerSec, wfx->wBitsPerSample, wfx->nChannels,
99 get_format_str(wfx->wFormatTag));
108 LPDIRECTSOUNDCAPTUREBUFFER dscbo;
110 DSBPOSITIONNOTIFY posnotify[NOTIFICATIONS];
111 HANDLE event[NOTIFICATIONS];
112 LPDIRECTSOUNDNOTIFY notify;
122 static int capture_buffer_service(capture_state_t* state)
127 DWORD capture_pos,read_pos;
129 rc=IDirectSoundCaptureBuffer_GetCurrentPosition(state->dscbo,&capture_pos,
131 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetCurrentPosition() failed: %s\n",
132 DXGetErrorString8(rc));
136 rc=IDirectSoundCaptureBuffer_Lock(state->dscbo,state->offset,state->size,
137 &ptr1,&len1,&ptr2,&len2,0);
138 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Lock() failed: %s\n",
139 DXGetErrorString8(rc));
143 rc=IDirectSoundCaptureBuffer_Unlock(state->dscbo,ptr1,len1,ptr2,len2);
144 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Unlock() failed: %s\n",
145 DXGetErrorString8(rc));
149 state->offset = (state->offset + state->size) % state->buffer_size;
154 static void test_capture_buffer(LPDIRECTSOUNDCAPTURE dsco,
155 LPDIRECTSOUNDCAPTUREBUFFER dscbo, int record)
161 capture_state_t state;
164 /* Private dsound.dll: Error: Invalid caps pointer */
165 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,0);
166 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetCaps() should "
167 "have returned DSERR_INVALIDPARAM, returned: %s\n",
168 DXGetErrorString8(rc));
170 /* Private dsound.dll: Error: Invalid caps pointer */
172 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
173 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetCaps() should "
174 "have returned DSERR_INVALIDPARAM, returned: %s\n",
175 DXGetErrorString8(rc));
177 dscbcaps.dwSize=sizeof(dscbcaps);
178 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
179 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetCaps() failed: %s\n",
180 DXGetErrorString8(rc));
181 if (rc==DS_OK && winetest_debug > 1) {
182 trace(" Caps: size = %ld flags=0x%08lx buffer size=%ld\n",
183 dscbcaps.dwSize,dscbcaps.dwFlags,dscbcaps.dwBufferBytes);
186 /* Query the format size. Note that it may not match sizeof(wfx) */
187 /* Private dsound.dll: Error: Either pwfxFormat or pdwSizeWritten must
189 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,NULL);
190 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetFormat() should "
191 "have returned DSERR_INVALIDPARAM, returned: %s\n",
192 DXGetErrorString8(rc));
195 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,&size);
196 ok(rc==DS_OK && size!=0,"IDirectSoundCaptureBuffer_GetFormat() should "
197 "have returned the needed size: rc=%s, size=%ld\n",
198 DXGetErrorString8(rc),size);
200 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,&wfx,sizeof(wfx),NULL);
201 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetFormat() failed: %s\n",
202 DXGetErrorString8(rc));
203 if (rc==DS_OK && winetest_debug > 1) {
204 trace(" Format: tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
205 wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
206 wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
209 /* Private dsound.dll: Error: Invalid status pointer */
210 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,0);
211 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCaptureBuffer_GetStatus() should "
212 "have returned DSERR_INVALIDPARAM, returned: %s\n",
213 DXGetErrorString8(rc));
215 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
216 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetStatus() failed: %s\n",
217 DXGetErrorString8(rc));
218 if (rc==DS_OK && winetest_debug > 1) {
219 trace(" Status=0x%04lx\n",status);
222 ZeroMemory(&state, sizeof(state));
225 state.buffer_size = dscbcaps.dwBufferBytes;
226 for (i = 0; i < NOTIFICATIONS; i++)
227 state.event[i] = CreateEvent( NULL, FALSE, FALSE, NULL );
228 state.size = dscbcaps.dwBufferBytes / NOTIFICATIONS;
230 rc=IDirectSoundCaptureBuffer_QueryInterface(dscbo,&IID_IDirectSoundNotify,
231 (void **)&(state.notify));
232 ok((rc==DS_OK)&&(state.notify!=NULL),
233 "IDirectSoundCaptureBuffer_QueryInterface() failed: %s\n",
234 DXGetErrorString8(rc));
238 for (i = 0; i < NOTIFICATIONS; i++) {
239 state.posnotify[i].dwOffset = (i * state.size) + state.size - 1;
240 state.posnotify[i].hEventNotify = state.event[i];
243 rc=IDirectSoundNotify_SetNotificationPositions(state.notify,NOTIFICATIONS,
245 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions() failed: %s\n",
246 DXGetErrorString8(rc));
250 ref=IDirectSoundNotify_Release(state.notify);
251 ok(ref==0,"IDirectSoundNotify_Release(): has %d references, should have "
257 rc=IDirectSoundCaptureBuffer_Start(dscbo,DSCBSTART_LOOPING);
258 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Start() failed: %s\n",
259 DXGetErrorString8(rc));
263 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
264 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_GetStatus() failed: %s\n",
265 DXGetErrorString8(rc));
266 ok(status==(DSCBSTATUS_CAPTURING|DSCBSTATUS_LOOPING),
267 "GetStatus: bad status: %lx\n",status);
271 /* wait for the notifications */
272 for (i = 0; i < (NOTIFICATIONS * 2); i++) {
273 rc=WaitForMultipleObjects(NOTIFICATIONS,state.event,FALSE,3000);
274 ok(rc==(WAIT_OBJECT_0+(i%NOTIFICATIONS)),
275 "WaitForMultipleObjects failed: 0x%lx\n",rc);
276 if (rc!=(WAIT_OBJECT_0+(i%NOTIFICATIONS))) {
277 ok((rc==WAIT_TIMEOUT)||(rc==WAIT_FAILED),
278 "Wrong notification: should be %d, got %ld\n",
279 i%NOTIFICATIONS,rc-WAIT_OBJECT_0);
281 if (!capture_buffer_service(&state))
285 rc=IDirectSoundCaptureBuffer_Stop(dscbo);
286 ok(rc==DS_OK,"IDirectSoundCaptureBuffer_Stop() failed: %s\n",
287 DXGetErrorString8(rc));
293 static BOOL WINAPI dscenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
294 LPCSTR lpcstrModule, LPVOID lpContext)
297 LPDIRECTSOUNDCAPTURE dsco=NULL;
298 LPDIRECTSOUNDCAPTUREBUFFER dscbo=NULL;
299 DSCBUFFERDESC bufdesc;
305 /* Private dsound.dll: Error: Invalid interface buffer */
306 trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
307 rc=pDirectSoundCaptureCreate(lpGuid,NULL,NULL);
308 ok(rc==DSERR_INVALIDPARAM,"DirectSoundCaptureCreate() should have "
309 "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
311 ref=IDirectSoundCapture_Release(dsco);
312 ok(ref==0,"IDirectSoundCapture_Release() has %d references, should "
316 rc=pDirectSoundCaptureCreate(lpGuid,&dsco,NULL);
317 ok((rc==DS_OK)||(rc==DSERR_NODRIVER),"DirectSoundCaptureCreate() failed: "
318 "%s\n",DXGetErrorString8(rc));
320 if (rc==DSERR_NODRIVER)
321 trace(" No Driver\n");
325 /* Private dsound.dll: Error: Invalid caps buffer */
326 rc=IDirectSoundCapture_GetCaps(dsco,NULL);
327 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_GetCaps() should have "
328 "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
330 /* Private dsound.dll: Error: Invalid caps buffer */
332 rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
333 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_GetCaps() should have "
334 "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
336 dsccaps.dwSize=sizeof(dsccaps);
337 rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
338 ok(rc==DS_OK,"IDirectSoundCapture_GetCaps() failed: %s\n",
339 DXGetErrorString8(rc));
340 if (rc==DS_OK && winetest_debug > 1) {
341 trace(" Caps: size=%ld flags=0x%08lx formats=%05lx channels=%ld\n",
342 dsccaps.dwSize,dsccaps.dwFlags,dsccaps.dwFormats,
346 /* Private dsound.dll: Error: Invalid size */
347 /* Private dsound.dll: Error: Invalid capture buffer description */
348 ZeroMemory(&bufdesc, sizeof(bufdesc));
351 bufdesc.dwBufferBytes=0;
352 bufdesc.dwReserved=0;
353 bufdesc.lpwfxFormat=NULL;
354 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
355 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
356 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
357 DXGetErrorString8(rc));
359 ref=IDirectSoundCaptureBuffer_Release(dscbo);
360 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
361 "should have 0\n",ref);
364 /* Private dsound.dll: Error: Invalid buffer size */
365 /* Private dsound.dll: Error: Invalid capture buffer description */
366 ZeroMemory(&bufdesc, sizeof(bufdesc));
367 bufdesc.dwSize=sizeof(bufdesc);
369 bufdesc.dwBufferBytes=0;
370 bufdesc.dwReserved=0;
371 bufdesc.lpwfxFormat=NULL;
372 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
373 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
374 "should have returned DSERR_INVALIDPARAM, returned %s\n",
375 DXGetErrorString8(rc));
377 ref=IDirectSoundCaptureBuffer_Release(dscbo);
378 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
379 "should have 0\n",ref);
382 /* Private dsound.dll: Error: Invalid buffer size */
383 /* Private dsound.dll: Error: Invalid capture buffer description */
384 ZeroMemory(&bufdesc, sizeof(bufdesc));
385 ZeroMemory(&wfx, sizeof(wfx));
386 bufdesc.dwSize=sizeof(bufdesc);
388 bufdesc.dwBufferBytes=0;
389 bufdesc.dwReserved=0;
390 bufdesc.lpwfxFormat=&wfx;
391 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
392 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
393 "should have returned DSERR_INVALIDPARAM, returned :%s\n",
394 DXGetErrorString8(rc));
396 ref=IDirectSoundCaptureBuffer_Release(dscbo);
397 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
398 "should have 0\n",ref);
401 /* Private dsound.dll: Error: Invalid buffer size */
402 /* Private dsound.dll: Error: Invalid capture buffer description */
403 init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
404 ZeroMemory(&bufdesc, sizeof(bufdesc));
405 bufdesc.dwSize=sizeof(bufdesc);
407 bufdesc.dwBufferBytes=0;
408 bufdesc.dwReserved=0;
409 bufdesc.lpwfxFormat=&wfx;
410 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
411 ok(rc==DSERR_INVALIDPARAM,"IDirectSoundCapture_CreateCaptureBuffer() "
412 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
413 DXGetErrorString8(rc));
415 ref=IDirectSoundCaptureBuffer_Release(dscbo);
416 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
417 "should have 0\n",ref);
420 for (f=0;f<NB_FORMATS;f++) {
422 init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
424 ZeroMemory(&bufdesc, sizeof(bufdesc));
425 bufdesc.dwSize=sizeof(bufdesc);
427 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
428 bufdesc.dwReserved=0;
429 bufdesc.lpwfxFormat=&wfx;
430 if (winetest_interactive)
431 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
432 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
433 ok(((rc==DS_OK)&&(dscbo!=NULL))||(rc==DSERR_BADFORMAT)||(rc==DSERR_ALLOCATED),
434 "IDirectSoundCapture_CreateCaptureBuffer() failed to create a "
435 "capture buffer: %s\n",DXGetErrorString8(rc));
437 test_capture_buffer(dsco, dscbo, winetest_interactive);
438 ref=IDirectSoundCaptureBuffer_Release(dscbo);
439 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
440 "should have 0\n",ref);
441 } else if (rc==DSERR_BADFORMAT) {
442 ok(!(dsccaps.dwFormats & formats[f][3]),
443 "IDirectSoundCapture_CreateCaptureBuffer() failed to create a "
444 "capture buffer: format listed as supported but using it failed\n");
445 if (!(dsccaps.dwFormats & formats[f][3]))
446 trace(" Format not supported: %s\n", format_string(&wfx));
447 } else if (rc==DSERR_ALLOCATED)
448 trace(" Already In Use\n");
451 /* try a non PCM format */
453 init_format(&wfx,WAVE_FORMAT_MULAW,8000,8,1);
454 ZeroMemory(&bufdesc, sizeof(bufdesc));
455 bufdesc.dwSize=sizeof(bufdesc);
456 bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
457 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
458 bufdesc.dwReserved=0;
459 bufdesc.lpwfxFormat=&wfx;
460 if (winetest_interactive)
461 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
462 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
463 ok((rc==DS_OK)&&(dscbo!=NULL),"IDirectSoundCapture_CreateCaptureBuffer() "
464 "failed to create a capture buffer: %s\n",DXGetErrorString8(rc));
465 if ((rc==DS_OK)&&(dscbo!=NULL)) {
466 test_capture_buffer(dsco, dscbo, winetest_interactive);
467 ref=IDirectSoundCaptureBuffer_Release(dscbo);
468 ok(ref==0,"IDirectSoundCaptureBuffer_Release() has %d references, "
469 "should have 0\n",ref);
473 /* Try an invalid format to test error handling */
475 init_format(&wfx,WAVE_FORMAT_PCM,2000000,16,2);
476 ZeroMemory(&bufdesc, sizeof(bufdesc));
477 bufdesc.dwSize=sizeof(bufdesc);
478 bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
479 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
480 bufdesc.dwReserved=0;
481 bufdesc.lpwfxFormat=&wfx;
482 if (winetest_interactive)
483 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
484 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
485 ok(rc!=DS_OK,"IDirectSoundCapture_CreateCaptureBuffer() should have failed "
486 "at 2 MHz %s\n",DXGetErrorString8(rc));
491 ref=IDirectSoundCapture_Release(dsco);
492 ok(ref==0,"IDirectSoundCapture_Release() has %d references, should "
499 static void capture_tests()
502 rc=pDirectSoundCaptureEnumerateA(&dscenum_callback,NULL);
503 ok(rc==DS_OK,"DirectSoundCaptureEnumerateA() failed: %s\n",
504 DXGetErrorString8(rc));
513 hDsound = LoadLibraryA("dsound.dll");
515 trace("dsound.dll not found\n");
518 pDirectSoundCaptureCreate=(void*)GetProcAddress(hDsound,"DirectSoundCaptureCreate");
519 pDirectSoundCaptureEnumerateA=(void*)GetProcAddress(hDsound,"DirectSoundCaptureEnumerateA");
520 if (!pDirectSoundCaptureCreate || !pDirectSoundCaptureEnumerateA)
522 trace("capture test skipped\n");