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
30 #include "wine/test.h"
37 #include "dsound_test.h"
39 #define NOTIFICATIONS 5
42 static char * format_string(WAVEFORMATEX* wfx)
46 sprintf(str, "%ldx%dx%d %s",
47 wfx->nSamplesPerSec, wfx->wBitsPerSample, wfx->nChannels,
48 wfx->wFormatTag == WAVE_FORMAT_PCM ? "PCM" :
49 wfx->wFormatTag == WAVE_FORMAT_MULAW ? "MULAW" :
50 wfx->wFormatTag == WAVE_FORMAT_IMA_ADPCM ? "IMA ADPCM" : "Unknown");
59 LPDIRECTSOUNDCAPTUREBUFFER dscbo;
61 DSBPOSITIONNOTIFY posnotify[NOTIFICATIONS];
62 HANDLE event[NOTIFICATIONS];
63 LPDIRECTSOUNDNOTIFY notify;
73 static int capture_buffer_service(capture_state_t* state)
78 DWORD capture_pos,read_pos;
80 rc=IDirectSoundCaptureBuffer_GetCurrentPosition(state->dscbo,&capture_pos,&read_pos);
81 ok(rc==DS_OK,"GetCurrentPosition failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
85 rc=IDirectSoundCaptureBuffer_Lock(state->dscbo,state->offset,state->size,&ptr1,&len1,&ptr2,&len2,0);
86 ok(rc==DS_OK,"Lock failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
90 rc=IDirectSoundCaptureBuffer_Unlock(state->dscbo,ptr1,len1,ptr2,len2);
91 ok(rc==DS_OK,"Unlock failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
95 state->offset = (state->offset + state->size) % state->buffer_size;
100 static void test_capture_buffer(LPDIRECTSOUNDCAPTURE dsco,
101 LPDIRECTSOUNDCAPTUREBUFFER dscbo, int record)
107 capture_state_t state;
110 /* Private dsound.dll: Error: Invalid caps pointer */
111 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,0);
112 ok(rc==DSERR_INVALIDPARAM,"GetCaps should have failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
114 /* Private dsound.dll: Error: Invalid caps pointer */
116 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
117 ok(rc==DSERR_INVALIDPARAM,"GetCaps should have failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
119 dscbcaps.dwSize=sizeof(dscbcaps);
120 rc=IDirectSoundCaptureBuffer_GetCaps(dscbo,&dscbcaps);
121 ok(rc==DS_OK,"GetCaps failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
123 trace(" Caps: size = %ld flags=0x%08lx buffer size=%ld\n",
124 dscbcaps.dwSize,dscbcaps.dwFlags,dscbcaps.dwBufferBytes);
127 /* Query the format size. Note that it may not match sizeof(wfx) */
128 /* Private dsound.dll: Error: Either pwfxFormat or pdwSizeWritten must be non-NULL */
129 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,NULL);
130 ok(rc==DSERR_INVALIDPARAM,
131 "GetFormat should have returned an error: rc=0x%lx(%s)\n",rc,DXGetErrorString8(rc));
134 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,NULL,0,&size);
135 ok(rc==DS_OK && size!=0,
136 "GetFormat should have returned the needed size: rc=0x%lx(%s) size=%ld\n",
137 rc,DXGetErrorString8(rc),size);
139 rc=IDirectSoundCaptureBuffer_GetFormat(dscbo,&wfx,sizeof(wfx),NULL);
140 ok(rc==DS_OK,"GetFormat failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
142 trace(" tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
143 wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
144 wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
147 /* Private dsound.dll: Error: Invalid status pointer */
148 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,0);
149 ok(rc==DSERR_INVALIDPARAM,"GetStatus should have failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
151 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
152 ok(rc==DS_OK,"GetStatus failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
154 trace(" status=0x%04lx\n",status);
157 ZeroMemory(&state, sizeof(state));
160 state.buffer_size = dscbcaps.dwBufferBytes;
161 for (i = 0; i < NOTIFICATIONS; i++)
162 state.event[i] = CreateEvent( NULL, FALSE, FALSE, NULL );
163 state.size = dscbcaps.dwBufferBytes / NOTIFICATIONS;
165 rc=IDirectSoundCaptureBuffer_QueryInterface(dscbo,&IID_IDirectSoundNotify,(void **)&(state.notify));
166 ok((rc==DS_OK)&&(state.notify!=NULL),"QueryInterface failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
170 for (i = 0; i < NOTIFICATIONS; i++) {
171 state.posnotify[i].dwOffset = (i * state.size) + state.size - 1;
172 state.posnotify[i].hEventNotify = state.event[i];
175 rc=IDirectSoundNotify_SetNotificationPositions(state.notify,NOTIFICATIONS,state.posnotify);
176 ok(rc==DS_OK,"SetNotificationPositions failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
180 rc=IDirectSoundNotify_Release(state.notify);
181 ok(rc==0,"Release: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
186 rc=IDirectSoundCaptureBuffer_Start(dscbo,DSCBSTART_LOOPING);
187 ok(rc==DS_OK,"Start: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
191 rc=IDirectSoundCaptureBuffer_GetStatus(dscbo,&status);
192 ok(rc==DS_OK,"GetStatus failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
193 ok(status==(DSCBSTATUS_CAPTURING|DSCBSTATUS_LOOPING),
194 "GetStatus: bad status: %lx\n",status);
198 /* wait for the notifications */
199 for (i = 0; i < (NOTIFICATIONS * 2); i++) {
200 rc=MsgWaitForMultipleObjects( NOTIFICATIONS, state.event, FALSE, 3000, QS_ALLEVENTS );
201 ok(rc==(WAIT_OBJECT_0+(i%NOTIFICATIONS)),"MsgWaitForMultipleObjects failed: 0x%lx\n",rc);
202 if (rc!=(WAIT_OBJECT_0+(i%NOTIFICATIONS))) {
203 ok((rc==WAIT_TIMEOUT)||(rc==WAIT_FAILED),"Wrong notification: should be %d, got %ld\n",
204 i%NOTIFICATIONS,rc-WAIT_OBJECT_0);
206 if (!capture_buffer_service(&state))
210 rc=IDirectSoundCaptureBuffer_Stop(dscbo);
211 ok(rc==DS_OK,"Stop: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
217 static BOOL WINAPI dscenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
218 LPCSTR lpcstrModule, LPVOID lpContext)
221 LPDIRECTSOUNDCAPTURE dsco=NULL;
222 LPDIRECTSOUNDCAPTUREBUFFER dscbo=NULL;
223 DSCBUFFERDESC bufdesc;
229 /* Private dsound.dll: Error: Invalid interface buffer */
230 trace("Testing %s - %s\n",lpcstrDescription,lpcstrModule);
231 rc=DirectSoundCaptureCreate(lpGuid,NULL,NULL);
232 ok(rc==DSERR_INVALIDPARAM,"DirectSoundCaptureCreate didn't fail: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
234 ref=IDirectSoundCapture_Release(dsco);
235 ok(ref==0,"IDirectSoundCapture_Release has %d references, should have 0\n",ref);
238 rc=DirectSoundCaptureCreate(lpGuid,&dsco,NULL);
239 ok((rc==DS_OK)||(rc==DSERR_NODRIVER),"DirectSoundCaptureCreate failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
243 /* Private dsound.dll: Error: Invalid caps buffer */
244 rc=IDirectSoundCapture_GetCaps(dsco,NULL);
245 ok(rc==DSERR_INVALIDPARAM,"GetCaps should have failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
247 /* Private dsound.dll: Error: Invalid caps buffer */
249 rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
250 ok(rc==DSERR_INVALIDPARAM,"GetCaps should have failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
252 dsccaps.dwSize=sizeof(dsccaps);
253 rc=IDirectSoundCapture_GetCaps(dsco,&dsccaps);
254 ok(rc==DS_OK,"GetCaps failed: 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
256 trace(" DirectSoundCapture Caps: size=%ld flags=0x%08lx formats=%05lx channels=%ld\n",
257 dsccaps.dwSize,dsccaps.dwFlags,dsccaps.dwFormats,dsccaps.dwChannels);
260 /* Private dsound.dll: Error: Invalid size */
261 /* Private dsound.dll: Error: Invalid capture buffer description */
262 ZeroMemory(&bufdesc, sizeof(bufdesc));
265 bufdesc.dwBufferBytes=0;
266 bufdesc.dwReserved=0;
267 bufdesc.lpwfxFormat=NULL;
268 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
269 ok(rc==DSERR_INVALIDPARAM,"CreateCaptureBuffer should have failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
271 ref=IDirectSoundCaptureBuffer_Release(dscbo);
272 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
275 /* Private dsound.dll: Error: Invalid buffer size */
276 /* Private dsound.dll: Error: Invalid capture buffer description */
277 ZeroMemory(&bufdesc, sizeof(bufdesc));
278 bufdesc.dwSize=sizeof(bufdesc);
280 bufdesc.dwBufferBytes=0;
281 bufdesc.dwReserved=0;
282 bufdesc.lpwfxFormat=NULL;
283 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
284 ok(rc==DSERR_INVALIDPARAM,"CreateCaptureBuffer should have failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
286 ref=IDirectSoundCaptureBuffer_Release(dscbo);
287 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
290 /* Private dsound.dll: Error: Invalid buffer size */
291 /* Private dsound.dll: Error: Invalid capture buffer description */
292 ZeroMemory(&bufdesc, sizeof(bufdesc));
293 ZeroMemory(&wfx, sizeof(wfx));
294 bufdesc.dwSize=sizeof(bufdesc);
296 bufdesc.dwBufferBytes=0;
297 bufdesc.dwReserved=0;
298 bufdesc.lpwfxFormat=&wfx;
299 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
300 ok(rc==DSERR_INVALIDPARAM,"CreateCaptureBuffer should have failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
302 ref=IDirectSoundCaptureBuffer_Release(dscbo);
303 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
306 /* Private dsound.dll: Error: Invalid buffer size */
307 /* Private dsound.dll: Error: Invalid capture buffer description */
308 init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
309 ZeroMemory(&bufdesc, sizeof(bufdesc));
310 bufdesc.dwSize=sizeof(bufdesc);
312 bufdesc.dwBufferBytes=0;
313 bufdesc.dwReserved=0;
314 bufdesc.lpwfxFormat=&wfx;
315 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
316 ok(rc==DSERR_INVALIDPARAM,"CreateCaptureBuffer should have failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
318 ref=IDirectSoundCaptureBuffer_Release(dscbo);
319 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
322 for (f=0;f<NB_FORMATS;f++) {
324 init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],formats[f][2]);
325 ZeroMemory(&bufdesc, sizeof(bufdesc));
326 bufdesc.dwSize=sizeof(bufdesc);
328 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
329 bufdesc.dwReserved=0;
330 bufdesc.lpwfxFormat=&wfx;
331 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
332 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
333 ok((rc==DS_OK)&&(dscbo!=NULL),"CreateCaptureBuffer failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
335 test_capture_buffer(dsco, dscbo, winetest_interactive);
336 ref=IDirectSoundCaptureBuffer_Release(dscbo);
337 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
341 /* try a non PCM format */
343 init_format(&wfx,WAVE_FORMAT_MULAW,8000,8,1);
344 ZeroMemory(&bufdesc, sizeof(bufdesc));
345 bufdesc.dwSize=sizeof(bufdesc);
346 bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
347 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
348 bufdesc.dwReserved=0;
349 bufdesc.lpwfxFormat=&wfx;
350 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
351 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
352 ok((rc==DS_OK)&&(dscbo!=NULL),"CreateCaptureBuffer failed to create a capture buffer 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
353 if ((rc==DS_OK)&&(dscbo!=NULL)) {
354 test_capture_buffer(dsco, dscbo, winetest_interactive);
355 ref=IDirectSoundCaptureBuffer_Release(dscbo);
356 ok(ref==0,"IDirectSoundCaptureBuffer_Release has %d references, should have 0\n",ref);
360 /* Try an invalid format to test error handling */
362 init_format(&wfx,WAVE_FORMAT_PCM,2000000,16,2);
363 ZeroMemory(&bufdesc, sizeof(bufdesc));
364 bufdesc.dwSize=sizeof(bufdesc);
365 bufdesc.dwFlags=DSCBCAPS_WAVEMAPPED;
366 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
367 bufdesc.dwReserved=0;
368 bufdesc.lpwfxFormat=&wfx;
369 trace(" Testing the capture buffer at %s\n", format_string(&wfx));
370 rc=IDirectSoundCapture_CreateCaptureBuffer(dsco,&bufdesc,&dscbo,NULL);
371 ok(rc!=DS_OK,"CreateCaptureBuffer should have failed at 2 MHz 0x%lx(%s)\n",rc,DXGetErrorString8(rc));
376 ref=IDirectSoundCapture_Release(dsco);
377 ok(ref==0,"IDirectSoundCapture_Release has %d references, should have 0\n",ref);
383 static void capture_tests()
386 rc=DirectSoundCaptureEnumerateA(&dscenum_callback,NULL);
387 ok(rc==DS_OK,"DirectSoundCaptureEnumerate failed: 0x%08lx(%s)\n",rc,DXGetErrorString8(rc));